如何让我的角色在Unity中向前移动?

时间:2022-02-19 20:48:31

I have a character in Unity which use a move script, to transform its position.x, y, or z everytime when I press the appropriate button. Now I would like to change my code to just rotate the character when the user press the "a" or "d" and don't move it, however if the user press the "w" button move it forward. I have already looked for it on the internet, and found out that I would need to use Vector3.forward somehow, but it doesn't work.

我在Unity中有一个使用移动脚本的角色,每当我按下相应的按钮时,它就会转换它的position.x,y或z。现在我想改变我的代码只是在用户按下“a”或“d”时旋转字符并且不移动它,但是如果用户按下“w”按钮则向前移动它。我已经在互联网上找了它,发现我需要以某种方式使用Vector3.forward,但它不起作用。

Here is my script:

这是我的脚本:

This is the script which actually moves the character: (It is attached to a Player object which contains the moveable characters)

这是实际移动角色的脚本:(它附加到包含可移动字符的Player对象)

public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;

Vector3 startPos;
Vector3 endPos;

bool firstInput;
public bool justJump;

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
        if (perc == 1) {
            lerpTime = 1;
            currentLerpTime = 0;
            firstInput = true;
            justJump = true;
        }
    }
    startPos = gameObject.transform.position;

    if (Input.GetButtonDown("right") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z);
    }
    if (Input.GetButtonDown("left") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z);
    }
    if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z + 0.5f);
    }
    if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
        endPos = new Vector3(transform.position.x, transform.position.y, transform.position.z - 0.5f);
    }

    if (firstInput == true) {
        currentLerpTime += Time.deltaTime * 5;
        perc = currentLerpTime / lerpTime;
        gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
        if (perc > 0.8f)  {
            perc = 1;
        }

        if (Mathf.Round(perc) == 1) {
            justJump = false;
        }
    }
}

}

And here is my "rotate script", which is attached to the moveable character(s) itself:

这是我的“旋转脚本”,它附加到可移动角色本身:

public class AnimationController : MonoBehaviour {

    Animator anim;
    public GameObject thePlayer;

    // Use this for initialization
    void Start () {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {
        Move moveScript = thePlayer.GetComponent<Move>();
        if (moveScript.justJump == true) {
            anim.SetBool("Jump", true);
        }
        else {
            anim.SetBool("Jump", false);
        }

        if (Input.GetButtonDown("right")) {
            gameObject.transform.rotation = Quaternion.Euler(0, 90, 0);
        }
        if (Input.GetButtonDown("left")) {
            gameObject.transform.rotation = Quaternion.Euler(0, -90, 0);
        }
        if (Input.GetButtonDown("up")) {
            gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
        }
        if (Input.GetButtonDown("down")) {
            gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
        }
    }
}

Could you give me please any help on this? (Preferably with code, if you can :) )

你能帮我个忙吗? (最好带代码,如果可以:))

1 个解决方案

#1


Assuming you want your character to move to where it is facing, and trying not to do a lot of changes to your code, this is what could be done:

假设您希望角色移动到它所面对的位置,并且尽量不对代码进行大量更改,这就是可以做到的:

First, attach both your script directly to your movable character.

首先,将您的脚本直接附加到您的可移动角色。

Then, change your Move script to:

然后,将Move脚本更改为:

public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;

Vector3 startPos;
Vector3 endPos;

bool firstInput;
public bool justJump;

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
        if (perc == 1) {
            lerpTime = 1;
            currentLerpTime = 0;
            firstInput = true;
            justJump = true;
        }
    }
    startPos = gameObject.transform.position;

    if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
        endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
    }
    if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
        endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
    }

    if (firstInput == true) {
        currentLerpTime += Time.deltaTime * 5;
        perc = currentLerpTime / lerpTime;
        gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
        if (perc > 0.8f)  {
            perc = 1;
        }

        if (Mathf.Round(perc) == 1) {
            justJump = false;
        }
    }
}

}

And your AnimationController script to

和你的AnimationController脚本

public class AnimationController : MonoBehaviour {

    Animator anim;
    public GameObject thePlayer;

    // Use this for initialization
    void Start () {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {
        Move moveScript = thePlayer.GetComponent<Move>();
        if (moveScript.justJump == true) {
            anim.SetBool("Jump", true);
        }
        else {
            anim.SetBool("Jump", false);
        }

        if (Input.GetButtonDown("right")) {
            gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
        }
        if (Input.GetButtonDown("left")) {
            gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
        }
    }
}

This will make your character rotate only when you press the left and right buttons, and move only when you press the up and down button. Althought this solves your problem, this is not the best way to write scripts for your character movement.

这将使您的角色仅在您按下左右按钮时旋转,并且仅在您按下向上和向下按钮时才会移动。虽然这解决了你的问题,但这不是为你的角色运动编写脚本的最佳方式。

I recommend you read more about the state machine design patter. This book by Robert Nystrom has a chapter about it and is really easy to read. Also, its free to read online :)

我建议你阅读有关状态机设计模式的更多信息。罗伯特·尼斯特罗姆(Robert Nystrom)的这本书有一章介绍它,非常容易阅读。另外,它可以免费在线阅读:)

#1


Assuming you want your character to move to where it is facing, and trying not to do a lot of changes to your code, this is what could be done:

假设您希望角色移动到它所面对的位置,并且尽量不对代码进行大量更改,这就是可以做到的:

First, attach both your script directly to your movable character.

首先,将您的脚本直接附加到您的可移动角色。

Then, change your Move script to:

然后,将Move脚本更改为:

public class Move : MonoBehaviour {
float lerpTime;
float currentLerpTime;
float perc = 1;

Vector3 startPos;
Vector3 endPos;

bool firstInput;
public bool justJump;

// Update is called once per frame
void Update () {
    if (Input.GetButtonDown("up") || Input.GetButtonDown("down") || Input.GetButtonDown("left") || Input.GetButtonDown("right")) {
        if (perc == 1) {
            lerpTime = 1;
            currentLerpTime = 0;
            firstInput = true;
            justJump = true;
        }
    }
    startPos = gameObject.transform.position;

    if (Input.GetButtonDown("up") && gameObject.transform.position == endPos) {
        endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
    }
    if (Input.GetButtonDown("down") && gameObject.transform.position == endPos) {
        endPos = transform.position + gameObject.transform.rotation * (new Vector3(0, 0, 0.5f));
    }

    if (firstInput == true) {
        currentLerpTime += Time.deltaTime * 5;
        perc = currentLerpTime / lerpTime;
        gameObject.transform.position = Vector3.Lerp(startPos, endPos, perc);
        if (perc > 0.8f)  {
            perc = 1;
        }

        if (Mathf.Round(perc) == 1) {
            justJump = false;
        }
    }
}

}

And your AnimationController script to

和你的AnimationController脚本

public class AnimationController : MonoBehaviour {

    Animator anim;
    public GameObject thePlayer;

    // Use this for initialization
    void Start () {
        anim = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {
        Move moveScript = thePlayer.GetComponent<Move>();
        if (moveScript.justJump == true) {
            anim.SetBool("Jump", true);
        }
        else {
            anim.SetBool("Jump", false);
        }

        if (Input.GetButtonDown("right")) {
            gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
        }
        if (Input.GetButtonDown("left")) {
            gameObject.transform.rotation = gameObject.transform.rotation * Quaternion.Euler(0, 90, 0);
        }
    }
}

This will make your character rotate only when you press the left and right buttons, and move only when you press the up and down button. Althought this solves your problem, this is not the best way to write scripts for your character movement.

这将使您的角色仅在您按下左右按钮时旋转,并且仅在您按下向上和向下按钮时才会移动。虽然这解决了你的问题,但这不是为你的角色运动编写脚本的最佳方式。

I recommend you read more about the state machine design patter. This book by Robert Nystrom has a chapter about it and is really easy to read. Also, its free to read online :)

我建议你阅读有关状态机设计模式的更多信息。罗伯特·尼斯特罗姆(Robert Nystrom)的这本书有一章介绍它,非常容易阅读。另外,它可以免费在线阅读:)