【问题标题】:Unity Rigidbody Click to moveUnity Rigidbody 点击移动
【发布时间】:2015-04-29 21:26:34
【问题描述】:

我想要一个脚本,当我在我的场景中单击时,我的播放器将旋转并添加一个力,并一直移动直到它到达我的场景中的单击点。

现在我可以使用 Vectors 并让我的播放器从一个点到另一个点工作。但我想修改它,这样我就可以使用物理并更好地了解我的玩家移动。就像让他加速开始移动并在他到达我的目标位置时减速

我的脚本现在看起来像这样

public GameObject isActive;
public float speed;
public Ray ray;
public Rigidbody rigidBody;



public Vector3 targetPoint;
// Use this for initialization
void Start ()
{
    targetPoint = transform.position;
}

// Update is called once per frame
void Update ()
{


}

void FixedUpdate()
{
    if (Input.GetMouseButton(0)) 
    {
        targetPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        ChangeRotationTarget ();
    }

    Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
    transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, speed * Time.deltaTime);

    rigidbody.position = Vector3.Lerp(transform.position, targetPoint, speed * Time.fixedDeltaTime);
}


void ChangeRotationTarget ()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Plane playerPlane = new Plane (Vector3.up, transform.position);

    float hitdist = 0.0f;

    if (playerPlane.Raycast (ray, out hitdist))
    {

        targetPoint = ray.GetPoint (hitdist);
    }

}

但是,当我运行它时,他只是从一个点滑到另一个点。无论我在刚体中施加的阻力或质量如何。

有人可以帮助我进行更改吗?或者指出我正确的方向

【问题讨论】:

    标签: c# unity3d physics rigid-bodies


    【解决方案1】:

    我没有时间对此进行测试,但它应该是您正在寻找的。只需将您的四元数和旋转代码应用于它。

    void FixedUpdate() {
    
        if (Input.GetMouseButton(0))
        {
            targetPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            if (rigidBody.position != targetPoint)
            {
                reachedTargetPoint = false;
                if (reachedTargetPoint == false)
                {
                    GetComponent<Rigidbody>().AddForce(speed);
                }
            }
    
            //zoneBeforeReachingTP should be how much units before reaching targetPoint you want to start to decrease your rigidbody velocity
            if (rigidBody.position == (targetPoint - zoneBeforeReachingTP))
            {
                //speedReductionRate should be how much of speed you want to take off your rigidBody at the FixedUpdate time rate
                rigidBody.velocity = speedReductionRate;
            }
                if(rigidBody.position == targetPoint)
                {
                    rigidBody.velocity = new Vector3(0, 0, 0);
                    reachedTargetPoint = true;
                }
                ChangeRotationTarget();
            }  
        }
    

    【讨论】:

    • 有趣,但代码似乎有点破。如果 (reachedTargetPoint == false) {} 将始终为假,因为您在上面的行中设置了它。但我想弄清楚如何使用逻辑:)
    • 是的!为错误道歉!我真的没有时间测试这个,但你是对的!
    【解决方案2】:

    那是因为您修改了rigidbody.position,并且您的逻辑覆盖了物理。相反,您需要做的是 ApplyForce 每个物理帧,可能是 ForceMode.ForceForceMode.Acceleration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多