【发布时间】:2020-05-23 17:04:46
【问题描述】:
我正在制作一个游戏,其中有一个我使用 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);
}
}
【问题讨论】:
标签: c# unity3d game-development