我正在尝试让击退在我的游戏中发挥作用,但出现错误 CS1003。该错误显示“Assets\Scripts\Enemy.cs(17,31):错误 CS1003:语法错误,预期为 ','”。这是我的代码:
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
Animator animator;
Rigidbody2D rb;
// Start is called before the first frame update
public float Health
{
set
{
if(value < health)
{
OnHit(Vector2 knockback);
}
health = value;
print(value);
if(health <= 0)
{
Defeated();
}
}
get
{
return health;
}
}
public float health = 20;
private void Start()
{
animator = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
}
public void Defeated()
{
animator.SetTrigger("Defeated");
}
public void OnHit()
{
//Apply force to enemy
rb.AddForce(knockback);
//animator.SetTrigger("IsHit");
}
public void RemoveEnemy()
{
Destroy(gameObject);
}
}`
我尝试的大多数事情只会产生更多错误。我觉得我错过了一些简单的东西。
在C#中,定义新方法时,需要提供参数类型:
public void MyMethod(int myParameter)
{
//code here
}
当您调用该方法时,您需要提供该类型的参数:
public void Main()
{
int customNumber = 5;
MyMethod(customNumber);
}
因此该错误告诉您编译器(或 ide 或其他)正在等待“Vector2”和“knockback”之间的逗号。它认为这是该方法的两个参数。所以你需要定义击退向量参数并将其传递给方法。
Vector2 knockback = new Vector2(); //I don't remember what parameters it want, so check Unity documentation
OnHit(knockback);