提问者:小点点

运算符“*”不能应用于“void”和“float”类型的操作数


***

使用System.集合;使用System.收藏。泛型;使用UnityEngine;公共类回击:Mono行为{公共浮点推力;公共浮点KnockTime;

// Start is called before the first frame update
void Start()
{
    
}
// Update is called once per frame
void Update()
{
    
}
private void OnTriggerEnter2D(Collider2D other)
{
    if(other.gameObject.CompareTag("enemy"))
    {
        Rigidbody2D enemy = other.GetComponent<Rigidbody2D>();
        if(enemy != null)
        {
            StartCoroutine(KnockCo(enemy));
        }
    }
}
private IEnumerator KnockCo(Rigidbody2D enemy)
{
    if(enemy != null)
    {
        Vector2 forceDirection = enemy.transform.position - transform.position;
        Vector2 force = forceDirection.Normalize() * thrust;
        
        enemy.velocity = force;
        yield return new WaitForSeconds(KnockTime);
       
       enemy.velocity = new Vector2();
       {
       }
    }
} }

共1个答案

匿名用户

forceDirection.Normize()修改forceDirection向量并返回space,而不是返回归一化的向量。因此,您需要将Normize()调用和乘法分解为单独的语句:

Vector2 forceDirection = enemy.transform.position - transform.position;
forceDirection.Normalize();
Vector2 force = forceDirection * thrust;