Unity C#在脚本之间进行调用时无法使我的功能正常工作

问题描述 投票:0回答:1

我已经阅读了大量教程,但是无法正常工作。是的,我是新手,所以这也许很明显。我读过我应该努力使我的代码保持独立和简单。因此,我试图将我的功能写在单独的脚本上,但随后毫无用处,然后将它们调用到我的主要玩家移动脚本中。但是无论出于什么原因,当我按下播放键时都没有任何反应。Pleeeeeease帮助我,请原谅我的无知。

最诚挚的问候

脚本1

public class Movement : MonoBehaviour
{

    public float speed = 5f;
    public Vector2 move;
    public Run run;
    public Walk walk;



    void Update()
    {
        walk.Walki();
        run.Sprint();

    }

脚本2

public class Walk : MonoBehaviour
{
    Rigidbody2D rb;
    float speed;
    public Vector3 move;
    private void Awake()
    {
        rb = GameObject.Find("Player").GetComponent<Rigidbody2D>();
        speed = GameObject.Find("Player").GetComponent<Movement>().speed;
    }
    public void Walki()
    {

        float mLeft = 1f;
        float mRight = -1f;

        Vector3 move = new Vector3(mLeft, mRight, 0f);

        if (Input.GetAxisRaw("Horizontal") > 0)
        {
            rb.transform.position += new Vector3(mLeft, 0f, 0f) * speed * Time.deltaTime;
        }

        if (Input.GetAxisRaw("Horizontal") < 0)
        {
            rb.transform.position += new Vector3(mRight, 0f, 0f) * speed * Time.deltaTime;
        }

    }
c# unity3d 2d
1个回答
0
投票
void Update(){
     gameObject.GetComponent<Walk>().Walkie();
}

您可以尝试这样调用它,并在Warkie方法中编写一个Debug.Log(“ walks”)。

© www.soinside.com 2019 - 2024. All rights reserved.