- Home /
AddForce to a point of a GameObject, not the center
I have an object, for simplicitaction call it a line, moving from A to B on high speed. It seems the Unitys build in physics don't work when you're directly altering the transform of the colliding body. To work around this I have the line save the position of where it starts to shoot and where it'll be at the end of the shot. Then it casts a ray, and if it collided I get all the collision data.
Now I'm stuck with two questions: how do I find what body is collided with? I tried
var hitBody = hit.rigidbody;
var hitObject = GameObject.Find("hitBody");
Debug.Log(""+hitObject);
But this just displays null. How would I, from the information returned by the Raycast, find the GameObject that this rigidbody or collider belongs to?
Secondly, and party following from the first, how would I add a force at the point the ray hit the GameObject? Adding this force from the center would be easy, but I have no idea how to add it at a certain point. Any help would be much appreciated! I try to return the favor by answering questions of people who know even less of unity than I do. :)
On the second part of your question, which might deserve it's own question, is this what you're asking:
how to add force to that point of the object the rayhits in such a way that it creates torque (twist) upon the GameObject based on that point and the direction of force as it applies to both the center of the GameObject's mass and it's current rotational rate and inertia?
Yes, exactly. I'm now going to play around with what Peter G suggested and see if that does what I hope it to do! :)
Answer by Peter G · Mar 23, 2011 at 02:19 AM
You aren't using GameObject.Find() correctly. And you really shouldn't use it in this case anyway.
var hitGameObject = hit.rigidbody.gameObject;
//That should do it.
As for your second question. RigidBody.AddForceAtPosition()
Thanks, as you can probably tell I'm an absolute newby to both scripting and unity. I'm learning pretty fast, but I still make a lot of rookie mistakes. But thanks for the help! :D
Your answer
Follow this Question
Related Questions
AI Players Movement for Rolling Ball Game 0 Answers
Physics.Raycast doesn't work with all collidables 1 Answer
Bounce not always triggering 0 Answers
RayCast Nearest Character 0 Answers