我在Unity游戏引擎上做了一个游戏,你可以控制一只鸟。当我把我的脚本附加到鸟上并点击播放时,鸟不会转动。鸟能够旋转,但实际上不能左右转动。我的上下运动也很像机器人,我想知道是否有办法改进它。我对Unity和编码很陌生,如果有帮助,我会非常感激。
我使用的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BirdController : MonoBehaviour
{
public float FlySpeed = 5;
public float YawAmount = 1;
public float PitchAmount = 1;
private float Yaw;
private float Pitch;
// Update is called once per frame
void Update()
{
//move forward
transform.position += transform.forward * FlySpeed * Time.deltaTime;
//inputs
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
//yaw, pitch, roll
Yaw += horizontalInput * YawAmount * Time.deltaTime;
Pitch += verticalInput * PitchAmount * Time.deltaTime;
float pitch = Mathf.Lerp(0, 50, Mathf.Abs(verticalInput)) * Mathf.Sign(verticalInput);
float roll = Mathf.Lerp(0, 70, Mathf.Abs(horizontalInput)) * -Mathf.Sign(horizontalInput);
//apply rotation.
transform.localRotation = Quaternion.Euler(Vector3.up * Yaw + Vector3.right * pitch + Vector3.forward * roll);
transform.localRotation = Quaternion.Euler(Vector3.right * Pitch + Vector3.left * pitch + Vector3.forward * roll);
}
}
1条答案
按热度按时间2w2cym1i1#
如果你说的“像机器人一样”是指鸟总是以相同的速度上下移动,那么尝试在
BirdController
类中添加x、y和z速度变量。当按下箭头键时,你可以改变速度,然后通过每个轴上的速度变量不断改变对象的位置。更改此:
对此:
我还没有测试过这段代码,所以它可能不会像预期的那样工作。