Reputation: 391
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.
Upvotes: 0
Views: 1804
Reputation: 855
Have you tried something like:
using UnityEngine;
using System.Collections;
public class MovingSphere : Monobehavior // Attatch to sphere1
{
public GameObject sphere1, sphere2; // Set these in the inspector
// Say sphere1 is at (-2, 0, 0)
// Say sphere2 is at ( 0, 0, 0)
void Update( )
{
if(sphere2.transform.position.x < 5) // If x-position of sphere2 < 5
{
sphere1.transform.Translate(0.1f, 0, 0); // Moves the first sphere
} // positively on the x-plane
}
}
I wrote this just now, so it isn't tested... However it should go something like this:
START SCENE:
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2)
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2)
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2)
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2)
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2)
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2)
[x] [-2] [-1] [00] [+1] [+2] [+4] [+5]
(s1) (s2) <Stop>
If the second sphere goes crazy when hit, you may need to lock the y-axis & z-axis, which can be done with a little checkbox in the inspector window.
Let us know if you need anything more!
Upvotes: 0
Reputation: 56536
For that to make sense with physics, and to do the same thing at any distance, I think you'd need sphere1 to be accelerating towards sphere2, (or start out with enough energy to go 10 units plus whatever friction it experiences) transfer all of its energy while stopping immediately, and then you'd need sphere2 to decelerate at the same rate after it was hit.
It might be simpler to move it yourself, using simplified or cartoonish physics, like with the spheres moving at a constant rate until they've gone the necessary distance.
Upvotes: 1