我在编程中是一个非常新的人,并且在此代码中遇到了多个错误,但是我不知道为什么[关闭]

问题描述 投票:-4回答:1
public class PlayerMovement : MonoBehaviour {

    public CharacterController2D controller;

    public float runspeed = 40f; 

    float Horizontalmovement = 0f;

    bool jump = false;
    // Update is called once per frame
    void Update() => Horizontalmovement = Input.GetAxisRaw("Horizontal") * runspeed;

    if(input.GetButtonDown("jump"))
{
        jump = true;
}


   void fixedupdate ()
{ 
    //move the character

    Controller.Move(horizontalmovement * Time.fixedDeltaTime, false, jump);
}

我正在遵循2018年的教程,所以可能是问题所在。链接:https://www.youtube.com/watch?v=dwcT-Dch0bA&t=911s

我在第21行有7个错误,在第23行有2个错误

c#
1个回答
0
投票

查看视频中的示例,您忘记了两件事:

  1. 将if语句放入update方法中。
  2. C#中的变量名区分大小写。

正如@Joelius所说。在尝试遵循更复杂的教程之前,查看一个更基本的示例并理解C#的基础可能是一个好主意。

下面是更正后的代码(尽管我不知道在示例的其余部分中还会发生什么,在您提供的代码段中,controller始终为空):

public class PlayerMovement : MonoBehaviour {

    public CharacterController2D controller;

    public float runspeed = 40f; 

    float horizontalmovement = 0f;

    bool jump = false;
    // Update is called once per frame
    void Update()  
    {
        horizontalmovement = Input.GetAxisRaw("Horizontal") * runspeed;

        if(input.GetButtonDown("jump"))
        {
            jump = true;
        }
    }

    void fixedupdate ()
    { 
         //move the character

         controller.Move(horizontalmovement * Time.fixedDeltaTime, false, jump);
    }
© www.soinside.com 2019 - 2024. All rights reserved.