RaycastHit + AddForceAtPosition

Hiya,

I have a sphere, and when the user clicks, it sends out a Raycast, now what I want to do, is not calculate the position the Raycast hit, but calculate the position behind the object where the Raycast hit, then apply a force there to “pull” the object towards me.

Any insight?

Cheers!

Edit: In C# if possible.

Hi, welcome to the forum!

I’m not sure what you mean when you say you want the position behind the object where the raycast hit. Any chance you can give a bit more detail?

Hi!
I’m not sure what you mean too, but if i understand well, you want hit the backface of the sphere?

Else you can use Physics.RaycastAll() who return a RaycastHit[ ].

Hiya,

Thanks for the replies thus far.

Basically, I’m making a kind of “gravity gun”, when the user left clicks, it does a raycast, and if it hits a rigidbody, it uses AddForceAtPosition to push the object away.

Now, when the user right clicks, I want it to pull or push the object towards them. The only way I can think of doing that is to use AddForceAtPosition to the opposite side of where the raycast hit, if this makes sense?

Or is there an easier way?

I guess pushing from the other side would look strange.
Try an inverted(negative) AddForceAtPosition for a pull effect.

Duh!

I’m so dense!

Fixed it…

If anyones interested, here is the code:

direction = hit.collider.rigidbody.transform.position - transform.position;
direction = -direction;
hit.collider.rigidbody.AddForceAtPosition(direction, hit.transform.position);