Rotation problem: Object rotates but shouldn't
Hello community!
I serached long in the forums for a solution and read the documentation again and again, but I'm now in a little dead end.
The situation: I have a spaceship. The spaceship should ALWAYS look to the direction it is flying. When it is not, it simply looks at the last direction it was flying. It can fly in 8 directions, with the keys W A S D and combinations. Now to bring a little bit quality in it, I want that the spaceships turns when you fly in another direction as before.
Lets have a look into the code. I have commented the important parts that causes the problem, the rest is just regular controlls stuff:
private float InputValueMoveX;
private float InputValueMoveZ;
private Vector3 moveDirection;
public int speed;
public int rotationSpeed;
public GameObject ship;
public Rigidbody shipRB;
void Start () {
}
void Update()
{
InputValueMoveX = Input.GetAxis("MoveX");
InputValueMoveZ = Input.GetAxis("MoveZ");
}
private void FixedUpdate ()
{
Movement();
Turn();
}
private void Movement()
{
float moveX = InputValueMoveX * speed * Time.deltaTime;
float moveZ = InputValueMoveZ * speed * Time.deltaTime;
transform.Translate(moveX, 0, moveZ);
saveMoveDirection(moveX, moveZ); // --------THIS-------
}
private void Turn() // ------ THIS FUNCTION------
{
if (Vector3.Dot(ship.transform.right.normalized, moveDirection.normalized) != 1) // MAIN PROBLEM IN THIS LINE
{
float turn = rotationSpeed * Time.deltaTime;
Quaternion rotation = Quaternion.Euler(0f, turn, 0f);
shipRB.MoveRotation(shipRB.rotation * rotation);
}
}
private void saveMoveDirection(float moveX, float moveZ) // -----THIS FUNCTION------
{
if(moveX != 0 || moveZ != 0)
{
moveDirection = new Vector3(moveX, 0, moveZ);
}
}
private bool KeyDown(string key)
{
if (Input.GetKey(key))
{
return true;
}
else
{
return false;
}
}
Now, as you can see I have a function calles saveMoveDirection(float moveX, float moveZ). The only thing it does is to save the last direction the ship was flying in Vector3 moveDirection, which will be later important in the function Turn() for ship rotation. It will not be overwritten when the ship stands and the Vector is (0, 0, 0).
Now if look at Turn() we see that there is an if function and in the if function is my rotation (with Quaternions). The rotation works without problem. The main problem here is the if function.
if (Vector3.Dot(ship.transform.right.normalized, moveDirection.normalized) != 1)
That means that if the local X axis (Vector3(1,0,0)) of my ship and Vector3 moveDirection show not in the same direction and have no points align, the ship will turn.
Logically it should turn so long until both Vectors are align and show in the same direction.
Now comes the funny thing. When I use a low value for rotationSpeed (for example 90f) in Turn() everything is fine and it works. But i think that too slow to turn around. When I double this value (180f) then suddenly the ship only holds on W (0°) A (90°) S (180°) D (270°) directions, but not the combined ones (45°, 135°, 225°, 315°) as if it would do too many steps per frame so that it does not catch them. If I go even further with the values, it holds on no direction. I took also 130f once and also did not hold at any point.
Is this some mathematical thing I did not understand? I really don't see what went wrong. There is some lack, but I don't find it.
I hope I could explain my problem well, and appreciate any help I can get.
Thank you people and have all a nice day!