下面的代码旋转一个对象并根据控制器上操纵杆的角度移动它们:
using UnityEngine;
using System.Collections;
public class PlayerControllerTEST : MonoBehaviour
{
public float moveSpeed;
public float h;
public float v;
public float moveX;
public float moveZ;
public Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
moveX = (h * moveSpeed) * Time.deltaTime;
moveZ = (v * moveSpeed) * Time.deltaTime;
transform.Translate(moveX, 0f, moveZ, Space.World);
Quaternion rotate = Quaternion.Euler(0f, (Mathf.Atan2(h, v) * Mathf.Rad2Deg), 0f);
Debug.Log(rotate);
rb.MoveRotation(rotate);
}
}
然而,当我松开操纵杆时,物体会回到0度的角度。我不知道在操纵杆基本上放开之前我怎么能保持角度。