【发布时间】:2019-07-07 14:02:22
【问题描述】:
我想在 Unity3D 中旋转一个立方体。当我按下键盘上的箭头左键时,立方体必须向左旋转。如果我向上推,立方体必须向上旋转。但是使用我的脚本,立方体向左旋转,然后左侧向上旋转。
这是当前状态:
这就是我想要的:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
public float smooth = 1f;
private Quaternion targetRotation;
// Start is called before the first frame update
void Start()
{
targetRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.UpArrow)){
targetRotation *= Quaternion.AngleAxis(90, Vector3.right);
}
if(Input.GetKeyDown(KeyCode.DownArrow)){
targetRotation *= Quaternion.AngleAxis(90, Vector3.left);
}
if(Input.GetKeyDown(KeyCode.LeftArrow)){
targetRotation *= Quaternion.AngleAxis(90, Vector3.up);
}
if(Input.GetKeyDown(KeyCode.RightArrow)){
targetRotation *= Quaternion.AngleAxis(90, Vector3.down);
}
transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, 10* smooth * Time.deltaTime);
}
}
【问题讨论】:
标签: c# unity3d rotation quaternions