Repelling a rigidbody in the direction an object is rotating
Posted
by
ndg
on Game Development
See other posts from Game Development
or by ndg
Published on 2014-06-05T20:00:51Z
Indexed on
2014/06/05
21:43 UTC
Read the original article
Hit count: 229
Working in Unity, I have a game object which I rotate each frame, like so:
void Update()
{
transform.Rotate(new Vector3(0, 1, 0) * speed * Time.deltaTime);
}
However, I'm running into problems when it comes to applying a force to rigidbodies that collide with this game objects sphere collider. The effect I'm hoping to achieve is that objects which touch the collider are thrown in roughly the same direction as the object is rotating. To do this, I've tried the following:
Vector3 force = ((transform.localRotation * Vector3.forward) * 2000) * Time.deltaTime;
collision.gameObject.rigidbody.AddForce(force, ForceMode.Impulse);
Unfortunately this doesn't always match the rotation of the object. To debug the issue, I wrote a simple OnDrawGizmos script, which (strangely) appears to draw the line correctly oriented to the rotation.
void OnDrawGizmos()
{
Vector3 pos = transform.position + ((transform.localRotation * Vector3.forward) * 2);
Debug.DrawLine(transform.position, pos, Color.red);
}
You can see the result of the OnDrawGizmos function below:
What am I doing wrong?
© Game Development or respective owner