-1
\$\begingroup\$

I have a spaceship I'm moving around on the screen, when the player moves the ship, it rotates on its axis. However, when the ship meets a clamped boundary(on the x-axis) it abruptly stops. How do I make it so that the ship will still rotate when it hits the clamped boundary?

void Controls(){
     Rotate();
     MoveShip();
 }
 void Rotate(){
     player.rotation = Quaternion.Euler((player.velocity.y * -pitchAngle), 0f, (player.velocity.x * -bankRotation));
 }
 void MoveShip(){
     float moveVertical = Input.GetAxis("Vertical");
     float moveHorizontal = Input.GetAxis("Horizontal");
     Vector3 bank = new Vector3(moveHorizontal, 0f, 0f);
     Vector3 pitch = new Vector3(0f, moveVertical, 0f);
     player.AddForce(bank * bankSpeed);
     player.AddForce(pitch * pitchSpeed);
     player.position = new Vector3(Mathf.Clamp(player.position.x, xMin, xMax), Mathf.Clamp(player.position.y, yMin, yMax), 0f);
     if((player.position.x == xMin) || (player.position.x == xMax) || (player.position.y == yMin) || (player.position.y == yMax)){
         player.velocity = new Vector3(0f, 0f, 0f);
         player.angularVelocity = new Vector3(0f, 0f, 0f);
     }
 }

The question that this one has been marked a duplicate of, the question I asked, does not address what this question is asking. That question asked "Why was the player object continuing to move passed the clamped boundaries?". This question is asking "How do I keep the player object rotating once it hist those clamped boundaries?" Two completely different questions asking two completely different things.

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

My guess: At the bottom of MoveShip() you are setting the velocity to zero, if the player is at the boundary. As your code in Rotate() depends on the x and y components of the velocity (which is set to zero in the previous update, when at the boundary), the rotation will be set to Quaternion.Euler(0,0,0) in these occasions

One way to solve this could be to use a Vector3 that holds some sort of "desired velocity" which is not clamped at the boundary. When you read from the desired velocity instead of the real velocity in order to rotate the ship, it should work

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .