首页 > 解决方案 > 我刚开始学习一些统一教程,但由于某种原因被卡住了

问题描述

我刚开始学习一些统一教程并被错误“没有给出与'My_Script.movePlayer(Vector3)的所需形式参数'direction'相对应的参数”所困扰,我不知道为什么?

public class My_Script : MonoBehaviour
{
    public float speed = 10.0f;//movement speed
    private Rigidbody rb;
    public Vector3 movement;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        // Keyboard Input
        float x = Input.GetAxis("Horizontal");
        float y = Input.GetAxis("Jump");
        float z = Input.GetAxis("Vertical");
        movement = new Vector3(x, y, z);
    }

    private void Fixedupdate()
    {
        movePlayer();
    }
    
    void movePlayer(Vector3 direction)
    {
        rb.velocity = direction * speed;
    }
}

标签: c#unity3d

解决方案


你的函数movePlayer(Vector3 direction)有一个vector3类型的参数,但是当你在中调用这个函数时FixedUpdate(),你忘了给它这个参数。所以你应该写:

private void Fixedupdate()
{
    movePlayer(movement);
}

这应该可以解决您的问题。


推荐阅读