Proper way to move Rigidbody GameObject

这一生的挚爱 提交于 2019-11-26 08:36:03

问题


I just started learning Unity. I tried to make a simple box move by using this script. The premise is, whenever someone presses \'w\' the box moves forward.

public class PlayerMover : MonoBehaviour {

public float speed;
private Rigidbody rb;


public void Start () {
    rb = GetComponent<Rigidbody>();
}

public void Update () {
    bool w = Input.GetButton(\"w\");

    if (w) {
        Vector3 move = new Vector3(0, 0, 1) * speed;
        rb.MovePosition(move);
        Debug.Log(\"Moved using w key\");

    }

}
}

Whenever I use this, the box doesn\'t move forward on a \'w\' keypress. What is wrong with my code? I thought it might be the way I have my Vector 3 move set up so I tried replacing the z-axis with speed, but that didn\'t work. Could someone tell me where I am messing up?


回答1:


You move Rigidbody with Rigidbody.MovePosition and rotate it with Rigidbody.MoveRotation if you want it to properly collide with Objects around it. Rigidbody should not be moved by their position, rotation or the Translate variables/function.

The "w" is not predefined like SherinBinu mentioned but that's not the only problem. If you define it and use KeyCode.W it still won't work. The object will move once and stop.

Change

Vector3 move = new Vector3(0, 0, 1) * speed;
rb.MovePosition(move);

to

tempVect = tempVect.normalized * speed * Time.deltaTime;
rb.MovePosition(transform.position + tempVect);

This should do it:

public float speed;
private Rigidbody rb;


public void Start()
{
    rb = GetComponent<Rigidbody>();
}

public void Update()
{
    bool w = Input.GetKey(KeyCode.W);

    if (w)
    {
        Vector3 tempVect = new Vector3(0, 0, 1);
        tempVect = tempVect.normalized * speed * Time.deltaTime;
        rb.MovePosition(transform.position + tempVect);
    }
}

Finally, I think you want to move your object with wasd key. If that's the case then use Input.GetAxisRaw or Input.GetAxis.

public void Update()
{
    float h = Input.GetAxisRaw("Horizontal");
    float v = Input.GetAxisRaw("Vertical");

    Vector3 tempVect = new Vector3(h, 0, v);
    tempVect = tempVect.normalized * speed * Time.deltaTime;
    rb.MovePosition(transform.position + tempVect);
}



回答2:


"w" is not predefined unless you explicitly define it. Use KeyCode.W




回答3:


Try this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMover : MonoBehaviour {

    public float speed;
    private Rigidbody rb;


    void Start () {
        rb = GetComponent<Rigidbody>();
    }

    void Update () {
        bool w = Input.GetKey(KeyCode.W);

        if (w) {
            Vector3 move = new Vector3(0, 0, 1) * speed *Time.deltaTime;
            rb.MovePosition(move);
            Debug.Log("Moved using w key");

        }

    }
}

Use Input.GetKey(KeyCode.W) for getting input.
EDIT NOTE: To move the object relative to its initial position use rb.MovePosition(transform.position+move) rather than rb.MovePosition(move)



来源:https://stackoverflow.com/questions/43714781/proper-way-to-move-rigidbody-gameobject

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!