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);
        }
    }
}

 

上一篇:【动画消消乐】HTML+CSS 自定义加载动画 057


下一篇:2021牛客寒假算法基础集训营3 I.序列的美观度