2016-05-07 4 views
0

Я пытаюсь получить 3D-объект, чтобы медленно возвращаться к его первоначальному вращению, когда игрок отпускает любой из влияющих клавиш.Unity3d вернуться к исходному ротации

Это то, что я до сих пор:

using UnityEngine; 
using System.Collections; 

public class ShipController : MonoBehaviour { 

    public float turnRate; 
    public Transform baseOrientation; 

    private Vector3 worldAxis, worldAxisRelative; 
    private Rigidbody rb; 

    void Start() { 
     rb = GetComponent<Rigidbody>(); 
     worldAxis = new Vector3 (0, 0, 0); 
     worldAxisRelative = transform.TransformDirection (worldAxis); 
    } 

    void Update() { 

    } 

    void FixedUpdate() 
    { 
     if (Input.GetKey (KeyCode.LeftArrow)) { 
      rb.transform.Rotate (Vector3.down * turnRate * Time.deltaTime); 
     } 
     if (Input.GetKey (KeyCode.RightArrow)) { 
      rb.transform.Rotate (Vector3.up * turnRate * Time.deltaTime); 
     } 
     if (Input.GetKey (KeyCode.UpArrow)) { 
      rb.transform.Rotate (Vector3.left * turnRate * Time.deltaTime); 
     } 
     if (Input.GetKey (KeyCode.DownArrow)) { 
      rb.transform.Rotate (Vector3.right * turnRate * Time.deltaTime); 
     } 
     axisAlignRot = Quaternion.FromToRotation (worldAxisRelative, worldAxis) ; 
     rb.transform.rotation = Quaternion.Slerp(rb.transform.rotation, axisAlignRot * transform.rotation, 1.0f * Time.deltaTime); 
    } 
} 

Но я не имею никакой удачи с ней. Как я могу заставить это работать?

ответ

1

Я думаю, что это должно быть относительно легко сделать, просто сохранив первоначальное вращение и используя Quaternion.RotateTowards. Для этого вам не нужно использовать твердое тело.

using UnityEngine; 
using System.Collections; 

public class ShipController : MonoBehaviour { 

    public Quaternion originalRotation; 

    void Start() { 
     originalRotation = transform.rotation; 
    } 

    void Update() 
    { 
     if (Input.GetKey (KeyCode.LeftArrow)) { 
      transform.Rotate (Vector3.down * turnRate * Time.deltaTime); 
     } 
     if (Input.GetKey (KeyCode.RightArrow)) { 
      transform.Rotate (Vector3.up * turnRate * Time.deltaTime); 
     } 
     if (Input.GetKey (KeyCode.UpArrow)) { 
      transform.Rotate (Vector3.left * turnRate * Time.deltaTime); 
     } 
     if (Input.GetKey (KeyCode.DownArrow)) { 
      transform.Rotate (Vector3.right * turnRate * Time.deltaTime); 
     } 
     transform.rotation = Quaternion.RotateTowards(transform.rotation, originalRotation, 1.0f * Time.deltaTime); 
    } 
} 

Это должно сработать. Возможно, вам придется немного изменить условия, потому что в настоящее время вы также поворачиваетесь к исходному вращению, если вы на самом деле нажимаете любую из четырех клавиш.

Смежные вопросы