AddRelativeForce Using Rotating Engine
I'm trying to create a ship that is propelled by an engine that rotates thrust around it in a circle while the ship itself remains fixed. It is a side scrolling flying game, so the ship moves up and down along the Z axis only.
I've created this hierarchy:
Player <- Has Rigidbody and movement script
Ship <- Doesn't rotate but flies around
Engine Mount <- Rotates to direct thrust
Engine Mesh
Particle System
The ship is a simple sphere and the engine rotates around the outside around the X axis to push along the Z axis.
I have the player oriented so that Z is forward (Y=90) and all the objects below are rotated 0 on all axis. I'm familiar with using AddRelativeForce() when the Rigidbody is controlling the vector but I can't seem to get this to work. I've tried using the Engine Mount's transform.forward but that doesn't work at all. It seems to just push one direction no matter how it is rotated.
All I want to do is apply a thrust force along the same axis as the Z axis of the engine mount. I can see that when I rotate the engine mount during game play that the Z axis is changing but the applied force direction doesn't seem to change, or at least isn't changing as expected. It never seems pointing in the same direction as the Z axis.
I tried to find the math to just grab the Z axis from the engine mount transform and use that to calculate the vector3 but I haven't had any luck. Any help would be appreciated. I've tried all sorts of orientation changes to flip around the engine mount but nothing has worked.
I'm sure that I'm missing something silly.
I got this to work, but I don't like it. Seems overly complicated.
public class ShipMover : MonoBehaviour {
[SerializeField] GameObject engineMount = default;
Rigidbody rb = default;
void Start() {
rb = GetComponent<Rigidbody>();
}
void Update() {
if (Input.GetKey(KeyCode.W)) {
Vector3 wrong = engineMount.transform.forward;
Vector3 correct = new Vector3(wrong.z, wrong.x, -wrong.y);
rb.AddRelativeForce(correct);
}
if(Input.GetKey(KeyCode.A)) {
engineMount.transform.Rotate(Vector3.left);
}
if (Input.GetKey(KeyCode.D)) {
engineMount.transform.Rotate(Vector3.right);
}
}
}
Your answer
Follow this Question
Related Questions
Batching (NOT Draw calls) 0 Answers
Animations get screwed up when I add OnAnimatorMove function 0 Answers
Photon VS Error 0 Answers
Unity 3D Balloons tied together (Physics) 0 Answers
Player keeps spinning after collision 0 Answers