一:演示视频
二:代码实现
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);
}
}
}