I have a 3d game object placed in a 2d scene (top-down view) in Unity. I want to add force to the object to move it in the direction it's facing. I attached a RigidBody2D component to it and when I use the AddForce() method, it's not working as I want it to i.e. it's moving on only one plane x or y but not in the facing direction. Please help me. Edit :- The game is my version of Asteroids. The ship is 3d and the asteroids are 2d. The scene is in 2d. I fixed the 'y' and 'z' rotation of the ship. Now how do I add force to the ship in the direction its facing ? The Update method -
void Update() {
float rotationInput = Input.GetAxis ("Rotate");
if (rotationInput != 0) {
float rotationAmount = RotateDegreesPerSecond * Time.deltaTime;
if (rotationInput < 0) {
rotationAmount *= -1;
}
ship.transform.Rotate (Vector3.down, rotationAmount);
}
Vector3 currentRotation = ship.transform.eulerAngles;
currentRotation.x *= Mathf.Deg2Rad;
thrustDirection = new Vector3 (Mathf.Sin(currentRotation.x) * -1, Mathf.Cos(currentRotation.x), Mathf.Cos(currentRotation.x));
}
The FixedUpdate method -
void FixedUpdate() {
if (Input.GetAxis ("Thrust") > 0) {
print (thrustDirection);
ship.AddRelativeForce (ThrustForce * thrustDirection, ForceMode.Force);
}
}
Thanks in advance.