- Home /
How do I make one object go in the same way and with the same speed as the other object?
Hi. I'm 'throwing' one object using the SpringJoint2D. How can I make other object go with the same speed as of this one? (and in the same direction)
It depends on how you want to use it, but the easiest thing would be to make it a child of the first object.
I want to just throw this object in the same direction and speed as of the other object and then let it go.
Can you suggest something else? I was trying to use AddForce() function but it doesn't really work the way I need it to.
It's still not clear to me exactly what you want to do, especially because with a spring joint the speed constantly changes, and the direction may also change all the time. So, from what you've said, I can only assume that you want the speed and direction of the object thrown first (object1), at the point in time that the second object (object2) is thrown.
So, here's some example code:
private Rigidbody rigidbody1;
private Rigidbody rigidbody2;
private Vector3 object1Velocity;
private void Awake()
{
rigidbody2 = GetComponent<Rigidbody>();
}
//$$anonymous$$ethod to call for throwing Object2
void Throw()
{
//You must tag Object1 so that you can find it from Object2
rigidbody1 = GameObject.FindGameObjectWithTag("Object1").GetComponent<Rigidbody>();
object1Velocity = rigidbody1.velocity; //read the speed and direction (velocity) of object1
rigidbody2.velocity = object1Velocity; // assign it to the velocity of object2.
}
Answer by BloodMarked · Dec 03, 2017 at 03:26 PM
there are multiple ways to do this, depending on what components you use and what behaviour you want
1) if you are using rigidbodies:
object2.GetComponent().velocity = object1.GetComponent().velocity;
2) storing the difference, then updating it every frame:
Start:
Vector3 difference = obj2.transform.position - obj1.trandform.position;
update:
obj2.trandform.position = obj1.transform.position + difference;
3) make it a child of the thrown object?
should be GetComponent<Rigidbody2D>()
ins$$anonymous$$d. but it vanishes when editing the answer
The angle brackets show only if you use the 101010 button.
Your answer
