- Home /
How to move an object over a distance in a direction?
Hi.I just want to do a collision with spheres.I've got two spheres and one of them moving to the other. At the beginning there is a distance between them and let's say the distance is 5.(Let's say sphere1 and sphere2).And when sphere1 hits to sphere2,sphere2 must move 5 units only.And this collision must materialize according to physics rules.How can i do that?I tried a lot of things but i'm very new to unity 3D.Because of that i couldn't find a way.Thanks for help.
Answer by Ben Ezard · Jun 19, 2012 at 02:54 PM
Add a rigidbody to each of the spheres. Make sure that both spheres have a collider component attached. On sphere1, set the velocity of the rigidbody so that it moves towards sphere2. When sphere1 hits sphere2, the collision will act according to the laws of physics.
Good luck to have a movement of precisely 5 units with that. An animation might be best.
Is there a way to just move sphere2 5 units in direction of sphere1?This will be enough for me now.
Answer by keld-oelykke · Jun 20, 2012 at 08:13 AM
I see 3 options depending on what product you want and what kind of solution you prefer:
1) Physics - good for some game mechanics - easy to start at - hard to tweak/fix: Follow Ben's approach and be aware that it will not 100% precise. A game physics engine is good at doing stuff fast and relative correct. It is not the best choice to simulate the law of physics in a precise way; this requires variable simulation steps.
2) Animations - good for visual presentation - easy to tweak - not so flexible: Follow Berengers approach e.g. using the Animation window in the Unity Editor. In this you can move the 2 spheres according to a common time line.
3) Code - expensive but flexible: If you can code scripts you can solve your problem in lots of ways. A code only solution is to encapsulate the behaviour of a sphere into a sphere script, add collision event handling (either by using colliders or math) and code the 100% impulse transfer as you want it to happen.
The best choice is dependent on your situation.
Is there a way to just move sphere2 5 units in the direction of sphere1?It will be enough for me now.
You can use the Translate method on the Transform component: http://unity3d.com/support/documentation/ScriptReference/Transform.html
Your answer
Follow this Question
Related Questions
C# Custom Collision Script (solved) 1 Answer
Physics.IgnoreCollision not working 0 Answers
Why can't my enemies walk up an incline? 1 Answer
Rigidbody Can't Remove Force Directly After Applied 0 Answers