简体   繁体   English

移动刚体游戏对象的正确方法

[英]Proper way to move Rigidbody GameObject

I just started learning Unity.我刚开始学习Unity。 I tried to make a simple box move by using this script.我尝试使用此脚本进行简单的框移动。 The premise is, whenever someone presses 'w' the box moves forward.前提是,每当有人按下“w”时,框就会向前移动。

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.每当我使用它时,该框不会在“w”按键上向前移动。 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.我想这可能是我设置Vector 3 move的方式,所以我尝试用速度替换 z 轴,但这没有用。 Could someone tell me where I am messing up?有人能告诉我我在哪里搞砸了吗?

You move Rigidbody with Rigidbody.MovePosition and rotate it with Rigidbody.MoveRotation if you want it to properly collide with Objects around it.如果您希望它与周围的对象正确碰撞,您可以使用Rigidbody.MoveRotation移动Rigidbody并使用Rigidbody.MovePosition旋转它。 Rigidbody should not be moved by their position, rotation or the Translate variables/function. Rigidbody不应通过其位置、旋转或 Translate 变量/函数移动。

The "w" is not predefined like SherinBinu mentioned but that's not the only problem. “w”不是像SherinBinu提到的那样预定义的,但这不是唯一的问题。 If you define it and use KeyCode.W it still won't work.如果你定义它并使用KeyCode.W它仍然不起作用。 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.最后,我认为您想使用 wasd 键移动对象。 If that's the case then use Input.GetAxisRaw or Input.GetAxis .如果是这种情况,则使用Input.GetAxisRawInput.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);
}

"w" is not predefined unless you explicitly define it. “w”不是预定义的,除非您明确定义它。 Use KeyCode.W使用 KeyCode.W

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.使用Input.GetKey(KeyCode.W)获取输入。
EDIT NOTE : To move the object relative to its initial position use rb.MovePosition(transform.position+move) rather than rb.MovePosition(move)编辑注意:要相对于其初始位置移动对象使用rb.MovePosition(transform.position+move)而不是rb.MovePosition(move)

Instead of making w a bool, you can use axis, also, in unity editor you should make it so the rigidbody movement is frozen您可以使用轴,而不是使w成为布尔值,而且,在统一编辑器中,您应该使刚体运动被冻结

here is some code这是一些代码

void update()
{
    rb.AddForce(Input.GetAxis("Horizontal"));
}

bool w = Input.GetKeyDown(KeyCode.W);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM