【问题标题】:How to move game object up at constant speed? It is not responding to gravity如何以恒定速度向上移动游戏对象?它对重力没有反应
【发布时间】:2015-12-07 11:11:20
【问题描述】:

我有这段代码,我已经在旧代码中移动了我的游戏对象,但在这段代码中我的玩家不想向上或向下移动。即使我在刚体设置中选择“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);
}
}

【问题讨论】:

    标签: c# unity3d scripting game-physics gameobject


    【解决方案1】:

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

    我推荐这个。

    GetComponent().AddForce(运动 * 速度 * 3); 注释此行并在达到目标速度时停止加速。 并启用“使用重力”

    或者你可以自己计算运动。但我不推荐它。

    'GetComponent().velocity = 运动 * 速度 / 2;' 评论这一行。自己实现重力。

    在现实世界中,您无法立即以“恒定速度”移动对象。 刚体是基于现实世界的物理学。所以后一种情况可能会导致物理故障。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多