- Home /
Rotational Force
Im making a tornado and in this script im using forces to pull objects into my tornado at an increased rate depending on distance. now I want to add rotational force. So what i want it to do is as objects get sucked into the tornado they will also start to rotate around the tornado the closer they get. I was wondering how i could acomplish this using addforce or something like that.
const float G = 667.4f;
public float TornadoPower;
public float TornadoRotation;
public float maxDistance;
public float MinDistance = 100;
public float RepelDistance = 200;
public Rigidbody rb;
void FixedUpdate()
{
GameOBJ[] gameObjects = FindObjectsOfType<GameOBJ>();
foreach (GameOBJ obj in gameObjects)
{
if (obj != this)
{
Attract(obj);
}
}
}
void Attract(GameOBJ objToAttract)
{
Rigidbody rbToAttract = objToAttract.GetComponent<Rigidbody>();
GameOBJ Attractforce = objToAttract.GetComponent<GameOBJ>();
Vector3 direction = rb.position - rbToAttract.position;
float distance = direction.magnitude;
float rotation = (TornadoRotation * Time.deltaTime) / Mathf.Pow(distance, 2);
if (distance <= maxDistance)
{
if (distance <= MinDistance)
{
return;
}
if (distance <= RepelDistance)
{
float forceMag = G * (TornadoPower * rbToAttract.mass * Attractforce.weight * .25f) / Mathf.Pow(distance, 2);
Vector3 Force = direction.normalized * forceMag;
rbToAttract.AddForce(-Force);
return;
}
float forceMagnitude = G * (TornadoPower * rbToAttract.mass * Attractforce.weight) / Mathf.Pow(distance, 2);
Vector3 force = direction.normalized * forceMagnitude;
rbToAttract.AddForce(force);
}
}
Comment