c# - 在统一 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;
推荐阅读
- github - github关键字是否区分大小写?
- google-bigquery - 如何使用 st_centroid 获取 bigquery 多边形的正确质心
- amazon-web-services - 通过公共子网访问私有子网上的 HTTP 服务器
- javascript - 使用 JavaScript 中的空间分区检测对象碰撞
- mysql - 错误:MySQL 意外关闭。XAMPP,如何修复损坏的表
- c# - 在将某个键添加到 ac# 字典之前验证某个键的值
- java - 在锁定屏幕时覆盖硬件音量按钮(设备处于唤醒状态)
- excel - 将特定部分的单词表导入excel
- bash - Ubuntu无人值守升级与谷歌云计算引擎上的启动脚本冲突
- utf-8 - VS Code 无法读取用其他编辑器编写的 unicode 语言