强烈的文字,所以我在做一个游戏,其中有一个我可以控制的飞机。可以的,但是我想当我抬起钥匙时飞机重新对准它原来的旋转方向。我编的鳕鱼是这个,但是不起作用。平面仅重新对齐一帧,然后再次“未对齐”。这是代码-**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class planemovement : MonoBehaviour
{
public int fspeed = 10;
float horizontal; float zrot;
float vertical; float yrot;
public float sense; public int lim = 0;
void Start()
{
}
// Update is called once per frame
void Update()
{
float rotz = Input.GetAxis("Vertical"); float roty = Input.GetAxis("Horizontal");
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * fspeed * Time.deltaTime);
transform.Translate(Vector3.right * sense * Time.deltaTime * horizontal*20f);
transform.Translate(Vector3.up * sense * Time.deltaTime * vertical);
zrot -= rotz;
yrot -= roty;
zrot = Mathf.Clamp(zrot, -lim, lim);
yrot = Mathf.Clamp(yrot, -lim, lim);
transform.localRotation = Quaternion.Euler(zrot, 0f, yrot);
}
}
Unity C#中的旋转通常是非常不稳定的,只有在正确使用四元数时才是精确的。
https://docs.unity3d.com/ScriptReference/Quaternion.html