- Home /
Pulling in another object using AddForce()
Hi.
I haven't spent much time with physics in Unity, thus I can't solve a basic problem. I've checked this forum and Uncle Google for a solution. Sadly, without success.
What I want to do is OBJECT1 pulls in OBJECT2, and both of them have rigidbody attached. While OBJECT1 is pulling OBJECT2 both of them are under Unity's physical laws. What I have now is:
Vector3 diff = object1Transform.position - object2Transform.position;
object2Transform.position += diff.normalized * 0.2f;
This is working, but of course it causes a conflict with physics (for example when OBJECT2 is changing position, its velocity doesn't). Therefore I would like to use AddForce(), but I can't get proper direction in which that object should be forced (and should be forced in direction of OBJECT1 position). I believe you can help me.
By the way, if you have any good resources for understanding physics in Unity, let me know please.
I appreciate any help.
To be honest that sounds like proper physics as if you were using a rope or tether. Do you use a hinge?
Try increasing mass or drag on object2
Thanks for you comment. The effect I want to achieve is similar to a rope or tether, but not exactly. I don't want those objects to be 'attached' I want OBJECT 1 to pull OBJECT2, something like OBJECT1 attracts OBJECT2. Therefore I don't use hinge. I almost solved problem using AddForceAtPosition() but for some reason OBJECT2 Y position is not changing, I will play with mass maybe
Can you $$anonymous$$oveToward?
Or even LookAt in which case you can Apply Force in one direction?
Answer by xpavelos · Sep 15, 2013 at 02:54 PM
Okay, I solved the problem.
What I did is I used AddForceAtPosition(dff.normalized, object2Transform.position, force), and turned off gravity of object2 while pulling. When pulling is finished object2 gravity is turned on again. Works perfectly!
But still, if you have any good resources for learning, post it please. ;)
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Rigid Character always falls (Physics) (No Root Motion) 1 Answer
Version 3.2 gives no OnTriggerExit when collider is parented 12 Answers
How to make Character Joint rotate towards a rest position? 0 Answers
How to make foot don't cross the ball? 0 Answers