我试图通过附加此脚本来模拟对象附近物体的重力影响;要做到这一点,我尝试向对象添加一个力量。但是我遇到other.attachedRigidbody.AddForce()
的问题我收到以下错误:
无法转换方法组' forceCalculation'非委托类型' UnityEngine.Vector3'。考虑使用括号来调用该方法。
public class planetaryForce : MonoBehaviour {
public float gravityPower;
public GameObject planetaryCore;
public float range;
void OnTriggerEnter(Collider other){
if (distance (other.gameObject) < range) {
other.attachedRigidbody.AddForce (forceCalculation(other.gameObject), ForceMode.Acceleration);
}
}
public float distance(GameObject other){
float distance = Vector3.Distance (other.transform.position, transform.position);
return distance;
}
public Vector3 direction(GameObject other){
Vector3 direction = transform.position - other.gameObject.transform.position;
return direction;
}
public float multiplier(GameObject other){
float multiplier = range - distance(other);
return multiplier;
}
public Vector3 forceCalculation(GameObject other){
forceCalculation = direction(other) * multiplier(other);
return forceCalculation;
}
}
答案 0 :(得分:1)
在forceCalculation()
中,您要返回forceCalculation()
方法,而应该做什么:
...
public Vector3 forceCalculation(GameObject other){
Vector3 calculation = direction(other) * multiplier(other);
return calculation;
}
答案 1 :(得分:1)
public Vector3 forceCalculation(GameObject other) {
forceCalculation = direction(other) * multiplier(other);
return forceCalculation;
}
第一行声明一个名为forceCalculation
的方法。
然后第二行尝试将Vector3分配给先前注册的名称forceCalculation
(这是一种方法)。
通过声明变量来解决此问题:
public Vector3 forceCalculation(GameObject other) {
Vector3 forceCalculation = direction(other) * multiplier(other);
return forceCalculation;
}