c# - 运算符“*”不能应用于“void”和“float”类型的操作数
问题描述
***
使用 System.Collections;使用 System.Collections.Generic;使用 UnityEngine;公共类击退: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(); { } } } }
解决方案
forceDirection.Normalize()
改变forceDirection
向量并返回void
,而不是返回归一化的向量。因此,您需要将Normalize()
调用和乘法拆分为单独的语句:
Vector2 forceDirection = enemy.transform.position - transform.position;
forceDirection.Normalize();
Vector2 force = forceDirection * thrust;
推荐阅读
- jquery - FullCalendar 清除选定的日期
- html - 是否可以在使用 endDate 脚本时启用日历中的 Next 按钮?
- java - 使用 Actionlistener 时 Arrayindex 超出范围
- excel - 使用 PhpSpreadsheet 将两个 excel 文件合并到一张表中
- hyperledger-fabric - 如何自定义 Hyperledger Fabric 测试网络?
- asp.net-core - 在现有的 AspNetUser 表中手动创建列是个好主意吗?
- ios - 具有相同 ViewController 和不同 ViewController 的 PageViewController
- linux - nginx 根据端口重新路由所有数据(nginx 代理)
- prometheus - 如何查询 Prometheus 中所有具有特定标签值的时间序列?
- powerbi - 更改客户状态