如何以恒定速度向上移动游戏对象?它对重力没有反应

How to move game object up at constant speed? It is not responding to gravity

我有这段代码,我在旧代码中移动了我的游戏对象,但在这段代码中我的玩家不想向上或向下移动。即使我在刚体设置中 select "UseGravity" ,游戏对象也不会向下移动!有什么问题?

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Boundary
{
public float xMin, xMax, zMin, zMax;
}


public class PlayerController : MonoBehaviour
{
public float speed;
public float tilt;
public Boundary boundary;

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");


    Vector3 movement = new Vector3(moveHorizontal, 0.025f, 0);

    GetComponent<Rigidbody>().AddForce(movement * speed * 3);
    GetComponent<Rigidbody>().velocity = movement * speed / 2;

    GetComponent<Rigidbody>().position = new Vector3
    (
        Mathf.Clamp(GetComponent<Rigidbody>().position.x, boundary.xMin, boundary.xMax),
        0.0f,
        Mathf.Clamp(GetComponent<Rigidbody>().position.z, boundary.zMin, boundary.zMax)
    );

    GetComponent<Rigidbody>().rotation = Quaternion.Euler(0.0f, 2f, GetComponent<Rigidbody>().velocity.x * -tilt);
}
}

由于您的代码强制速度常数,addForce() 和 'useGravity' 将不起作用

我推荐这个。

GetComponent().AddForce(movement * speed * 3); comment this line and stop acceleration when it reaches target speed. and enable 'use gravity'

或者你也可以自己算一下动静。但我不推荐它。

'GetComponent().velocity = movement * speed / 2;' comment this line. implement the gravity by yourself.

在现实世界中,您无法立即移动对象 'constant speed'。 刚体基于现实世界的物理学。所以后一种情况可能会导致物理故障。