我在 Youtube 上观看了一些教程,了解我想要在 Unity 项目中使用的脚本。 在 28:13 分钟内https://www.youtube.com/watch?v=b87iVclVT2E,您可以看到脚本的重要部分,这是我的副本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public CharacterController2D controller;
public Animator animator;
public float runSpeed = 40f;
float horizontalMove = 0f;
bool jump = false;
bool crouch = false;
void Update(){
horizontalMove = Input.GetAxisRaw ("Horizontal") * runSpeed;
animator.SetFloat ("Speed", Mathf.Abs (horizontalMove));
if (Input.GetButtonDown("Jump"))
{
jump = true;
animator.SetBool("IsJumping", true);
}
if (Input.GetButtonDown("Crouch"))
{
crouch = true;
} else if (Input.GetButtonUp("Crouch"))
{
crouch = false;
}
}
public void OnLanding ()
{
animator.SetBool ("IsJumping", false);
}
public void OnCrouching (bool isCrouching);
{
animator.SetBool("IsCrouching", isCrouching);
}
void FixedUpdate ()
{
controller.Move (horizontalMove * Time.fixedDeltaTime, crouch, jump);
jump = false;
}
}
当我尝试时,Unity 响应了以下错误:
Assets/PlayerMovement.cs(42,2): error CS9010: Primary constructor body is not allowed
Assets/PlayerMovement.cs(47,2): error CS9010: Primary constructor body is not allowed
Assets/PlayerMovement.cs(47,2): error CS8041: Primary constructor body is not allowed
如何修复这些错误?
问题是在这行代码中不应该出现分号:
public void OnCrouching (bool isCrouching); //there should not be a semicolon here
{
animator.SetBool("IsCrouching", isCrouching);
}
更改为
public void OnCrouching (bool isCrouching)
{
animator.SetBool("IsCrouching", isCrouching);
}