其他分享
首页 > 其他分享> > Unity中实现小球反弹

Unity中实现小球反弹

作者:互联网

一:演示视频


二:代码实现

using UnityEngine;

public class Player : MonoBehaviour
{
    private void Update()
    {
        transform.Translate(transform.forward * Time.deltaTime * 30, Space.World);
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Up")
        {
            float rotateY = transform.rotation.eulerAngles.y;
            float targetY = (90 - rotateY) + 90 - rotateY;
            transform.Rotate(Vector3.up * targetY);
        }
        if (other.gameObject.tag == "Down")
        {
            float rotateY = transform.rotation.eulerAngles.y;
            float targetY = (90 - rotateY) + 90 - rotateY;
            transform.Rotate(Vector3.up * targetY);
        }
        if (other.gameObject.tag == "Right")
        {
            float rotateY = transform.rotation.eulerAngles.y;
            float targetY = (90 - rotateY) + 270 - rotateY;
            transform.Rotate(Vector3.up * targetY);
        }
        if (other.gameObject.tag == "Left")
        {
            float rotateY = transform.rotation.eulerAngles.y;
            float targetY = (90 - rotateY) + 270 - rotateY;
            transform.Rotate(Vector3.up * targetY);
        }
    }
}

 

标签:float,小球,transform,rotateY,Unity,other,targetY,90,反弹
来源: https://blog.csdn.net/LLLLL__/article/details/117266549