[英]rotation to original position in unity 3d
我正在制作一個游戲,其中有一個我使用 wasd 鍵控制的飛機,它可以旋轉和平移。 到目前為止,它很好,但我希望當我舉起鑰匙時飛機能重新調整到原來的旋轉方向。 我編寫的代碼是這樣的,但它不起作用。 平面僅重新對齊一幀,然后再次“未對齊”。 這是代碼-**
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# 通常非常不穩定,唯一准確的情況是正確使用四元數。
public Quaternion startQuaternion;
void Start() {
startQuaternion = transform.rotation;
}
//when you want to reset to original
transform.rotation = startQuaternion;
我不太明白,但如果您使用剛體,您可以嘗試使用“Quaternion.Lerp”和 MoveRotation。 Quaternion.Lerp 具有三個參數,並創建從點 A 到 B 的旋轉,速度為 ugual 到 T(T 從 0 變為 1)。
var currentRot = transform.rotation
var desired Rot = rotation on which the plane must be aligned
Quaternion RotPlane = Quaternion.Lerp (currentRot, desiredRot, 0.5)
MoveRotation(RotPlane)
您可以使用 if (Input.GetKeyUp) 並將腳本放在它下面,因此每次釋放按鈕時,飛機都會返回所需的旋轉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.