here is a script i put on a trigger collider on my tornado which does the physics for my tornado. there are no errors in the script. but it isnt making my rigidbodies do anything. whats the problem?
Vector3 origin; //Should be set to Tornado's base
[SerializeField]
float tornadoHeight = 40f;
[SerializeField]
float strength = 20f;
[SerializeField]
float accelerationFactor = 1.1f;
[SerializeField]
float yPush = .2f;
List entities = new List();
float sqrStrength = 400f;
void OnValidate() {
sqrStrength = strength * strength;
}
void FixedUpdate() {
foreach (Rigidbody entity in entities) {
Vector3 relPos = entity.transform.position - origin;
if (relPos.y > tornadoHeight || relPos.x * relPos.x + relPos.z * relPos.z > relPos.y * relPos.y)
continue;
entity.velocity = entity.velocity * accelerationFactor;
float sqrVelocity = entity.velocity.sqrMagnitude;
if (sqrVelocity > sqrStrength)
sqrVelocity = sqrStrength;
float sqrRadius = relPos.sqrMagnitude;
Vector3 radialAcceleration = - relPos * sqrVelocity / sqrRadius;
radialAcceleration.y = radialAcceleration.y + yPush;
entity.AddForce(radialAcceleration);
}
entities.Clear();
}
void OnTriggerStay(Collider other) {
Rigidbody rigidbody = other.gameObject.GetComponent();
if (rigidbody != null && !entities.Contains(rigidbody))
entities.Add(rigidbody);
}
}
↧