【问题标题】:Operator '*' cannot be applied to operands of type 'void' and 'float'运算符“*”不能应用于“void”和“float”类型的操作数
【发布时间】:2021-07-27 08:56:35
【问题描述】:
***

使用 System.Collections;使用 System.Collections.Generic;使用 统一引擎;公共类击退:MonoBehaviour { 公共浮动推力; 公共浮动敲击时间;

// 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();
       {
       }
    }
} }

【问题讨论】:

    标签: c#


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-12
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      • 2014-05-27
      • 2015-01-25
      • 2011-12-31
      • 2011-04-03
      相关资源
      最近更新 更多