How to Bump back (opposite direction of being hit) Addforce

I want a sphere to bump back on collision with another object. So in the opposite direction of the point its being hit.

With my current code the sphere does bouce back. however it always goes backwards. (Not always the right direction from where its being hit)

here’s my code:

function OnCollisionStay(collision : Collision) {
		// Check if the collider we hit has a rigidbody
		// Then apply the force
		if (collision.rigidbody) {
			collision.rigidbody.AddForce (-Vector3.forward * 5);
		}
	}

I don’t really know if there’s a function to do that, but what I would do is:

  1. Comparing the object’s position with the position of the one that collided with.
  2. Store that difference in a new Vector3. Something like Vector3 diff = transform.position - collision.gameObject.transform.position;
  3. Replace Vector3.forward with that new Vector3.

I haven’t even tested it, but it’s an idea I just got.

Hope it helps.

Starting from the Collision, the collision contains contact data, which contains ContactPoint information, which contains normal information for the impact. From there, you can Reflect the vector against the normal of the impact to skew your new direction.