How to move a force along with an object?
Hi,
I'm trying to do something fairly easy over here, but can't seem to figure it out. I'd like to move a force along with an object.
Let me explain this in a bit more detail:
I've got a rectangle that has a force applied on it when certain buttons are pressed. It is possible that because of the input of the force, the object gets rotated. If this happens, I'd like to rotate the force accordingly. Here is a (horrible) drawing of my goal. I'm currently working with this code:
// Rotation in radians
float rocketRotation = rocket.transform.eulerAngles.z * Mathf.PI / 180;
Vector3 forcePositionRotation = new Vector3(rocket.transform.position.x * Mathf.Cos(rocketRotation) + (rocket.transform.position.y + 50) * Mathf.Sin(rocketRotation), (rocket.transform.position.y + 50) * Mathf.Cos(rocketRotation) - rocket.transform.position.x * Mathf.Sin(rocketRotation), rocket.transform.position.z);
if (Input.GetKey(KeyCode.RightArrow))
{
rocketBody.AddForceAtPosition(new Vector3(10, 0, 0), forcePositionRotation);
forceIndicator.transform.position = forcePositionRotation;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
rocketBody.AddForceAtPosition(new Vector3(-10, 0, 0), forcePositionRotation);
forceIndicator.transform.position = forcePositionRotation;
}
So I've captured the rotation angle in radians and tried to get the new x and y positions with the formula:
y' = y*cos(a) - x*sin(a)
x' = y*sin(a) + x*cos(a)
I've added 50 to the y coordinate because I'd like my force to be applied to the top of the rectangle which is 100 large.
I hope the problem is clear!
Thanks in advance,
Bram
Your answer
Follow this Question
Related Questions
Walking around a sprite 1 Answer
Why does the player not move around planet with gravity ? 1 Answer
Rotate the player around the z axis, using a mouse look type script 0 Answers
Glider sim with CharacterController (no rigid body allowed) 0 Answers
How to Set a Single Axis Rotation of a GameObject? 0 Answers