【问题标题】:How can I make my character move only forward in Unity?如何让我的角色在 Unity 中仅向前移动?
【发布时间】:2015-05-08 20:09:10
【问题描述】:

我在 Unity 中有一个使用移动脚本的角色,每次按下相应的按钮时都会转换其 position.x、y 或 z。现在我想将我的代码更改为仅在用户按下“a”或“d”而不移动它时旋转字符,但是如果用户按下“w”按钮将其向前移动。 我已经在网上找过了,发现我需要使用 Vector3.forward,但它不起作用。

这是我的脚本:

这是实际移动角色的脚本: (它附加到一个包含可移动角色的 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;
        }
    }
}

}

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

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);
        }
    }
}

你能给我任何帮助吗? (最好有代码,如果可以的话:))

【问题讨论】:

  • 那么你有什么问题呢?它会动吗?你有什么错误吗?

标签: unity3d


【解决方案1】:

假设你想让你的角色移动到它所面对的地方,并且尽量不对你的代码做很多改动,可以这样做:

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

然后,将您的移动脚本更改为:

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;
        }
    }
}

}

还有你的 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);
        }
    }
}

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

我建议您阅读有关状态机设计模式的更多信息。 Robert Nystrom 的 book 有一章关于它,并且非常容易阅读。此外,它可以免费在线阅读:)

【讨论】:

  • 感谢您的帮助!还有一个问题,我想你可以为我回答,我怎样才能在不改变位置的情况下旋转我的角色?我的意思是,如果我用这种方法旋转我的角色,它会稍微改变它的位置,而不仅仅是像我在 y 轴上旋转它时那样围绕它的中心旋转。
  • 我测试了我编写的脚本,但在旋转时看不到任何位置变化...您是否删除了那些包含 if 行为的块Move 脚本中的 i>left 和 right 按钮?旋转时变换位置坐标会改变吗?如果它们没有改变,可能你的对象没有集中,或者你的跳跃动画正在改变一些东西。这可能吗?我可能需要更多信息来了解正在发生的事情,以便为您提供帮助
  • 感谢它现在可以正常工作,这是我的模型导入的问题,因为它的轴心点没有在适当的点上。 :) 谢谢,我会看看那本书 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多