- Home /
The question is answered, right answer was accepted
Movement Relative to Rotated Parent
Greetings,
I'm trying to create a child-object (rigidbody2D) inside another rigidbody2D. I can move them seperately which works fine. The parent object is rotating when pressing left and right, the child object is moving directly over the X and Y axis (so left and right, no rotation involved).
The big problem is that when parent objects are rotated, the child objects are still moving in straight lines X and Y. So relative to the parent they are moving in wrong directions.
public void moveRight()
{
rigidbody2D.velocity = new Vector2(1, 0);
}
public void moveLeft()
{
rigidbody2D.velocity = new Vector2(-1, 0);
}
public void moveUp()
{
rigidbody2D.velocity = new Vector2(0, 1);
}
public void moveDown()
{
rigidbody2D.velocity = new Vector2(0, -1);
}
public void moveStop()
{
rigidbody2D.velocity = new Vector2(0, 0);
}
The problem is that the velocity is set not taking into account the parent object.
rotationInDegrees = Mathf.Atan2(attachedTo.rigidbody2D.position.y,
attachedTo.rigidbody2D.position.x) * Mathf.Rad2Deg;
I can use this with Cos and Sin. But I never can get it quite right.
Instead of directly setting the velocity I found/came up with the following code, but again it is not taking into account the parent's rotation.
void FixedUpdate(){
attachedTo.rigidbody2D.position.x) * Mathf.Rad2Deg;
rigidbody2D.velocity = new Vector2(Mathf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f),
Mathf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f));
I'm currently lost as I want the character to move by the last piece of code but also take into consideration the parent's angle to move in the right direction... Any piece of advice on how to do this? Or possibly an answer?! Ive been looking but haven't found an answer on the interwebs... Sidequestion: can I force the rigidbody2D to stay inside the parent?
Answer by gfoot · Feb 24, 2015 at 08:09 PM
Physics is not performed relative to the parent - it all happens externally, then the world-space values for rigid body positions get put back into the objects.
One option you have is to use constraints to constrain the child to the parent's axis. This will give a physically plausible result when the parent rotates while the child is already moving. Look them up if it sounds useful.
You might also want to apply your forces relative to the parent, so instead of using Vector2(1, 0) you can transform it using the parent's transform, to get a new world-space vector that's not just along the world X axis.
Vector2 worldVector = parent.transform.TransformVector(localVector);
It really depends what you're trying to do, and actually I find myself doubting that you really want physics involved here at all. You may be better off just not using rigidbody2d at all.
$$anonymous$$aking the child restrict to parent's axis was already happening. This indeed makes the child-object stick to the parent when controlling the parent. When controlling the child-object and adjusting velocity of the rigidbody2d it is also moving relative from the parent-object when you make it Extrapolate.
However it is not taking into account the rotation of the parent's object when controlling the child object. I have tried out multiplaying by the parent's transform but what part of the transform you want me to use. The position, scale, rotation? As I'm not sure how to use the transform.rotation as an object to multiply the velocity by.
I've tried something like below but it's giving me far from any good results.
rotation = $$anonymous$$athf.Atan2(attachedTo.rigidbody2D.position.y, attachedTo.rigidbody2D.position.x);
rigidbody2D.velocity =
Vector2.Scale( new Vector2($$anonymous$$athf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f), $$anonymous$$athf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f)) ,
new Vector2($$anonymous$$athf.Cos(rotation), $$anonymous$$athf.Sin(rotation)) );
Use attachedTo.transform.TransformVector(localVector) to get a world-space version of a vector relative to attachedTo.
You sir, amazing! For anyone else wondering what the final solution was for the problem below is the code.
void FixedUpdate(){
rigidbody2D.velocity = attachedTo.transform.TransformVector( new Vector2($$anonymous$$athf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f),
$$anonymous$$athf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f))
);
}
Answer was given by gfoot:
"Use attachedTo.transform.TransformVector(localVector) to get a world-space version of a vector relative to attachedTo."
void FixedUpdate(){
rigidbody2D.velocity = attachedTo.transform.TransformVector( new Vector2($$anonymous$$athf.Lerp(0, Input.GetAxis("Horizontal") * 5, 0.8f),
$$anonymous$$athf.Lerp(0, Input.GetAxis("Vertical") * 5, 0.8f))
);
}
Follow this Question
Related Questions
Unity C# 2D Adding Velocity on rotation 1 Answer
Projetil rotation in straight line trajectory 2 Answers
2D Geometry dash-like ship physics 0 Answers