我的鸟控制器没有按预期工作

问题描述 投票:0回答:1

我正在 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);

    }
}

unity3d
1个回答
0
投票

如果“像机器人一样”是指鸟总是以相同的速度上下移动,请尝试将 x、y 和 z 速度变量添加到

BirdController
类。当按下箭头键时,你可以改变那个速度,然后通过每个轴上的速度变量不断改变物体的位置。

改变这个:

transform.position += transform.forward * FlySpeed * Time.deltaTime;

float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

对此:

transform.position += new Vector3(xVelocity, yVelocity, zVelocity) * FlySpeed * Time.deltaTime;

xVelocity += Input.GetAxis("Horizontal");
yVelocity += Input.GetAxis("Vertical");
zVelocity = 0;

我还没有测试这段代码,所以它可能无法按预期工作。

© www.soinside.com 2019 - 2024. All rights reserved.