The question is answered, right answer was accepted
Rotate Around Function not working as intended
Hi,
So I'm making a space game, with the camera using the "Rotate Around" function. For the keys that go "left and right" the function works as intended. My camera will rotate around the ship either in a left or right direction. The up and down is what is giving me trouble.
When I am directly behind the ship or in front of it, up and down will take me over/under the ship (as intended). My problem is when I am look at the ship from the port of starboard sides. When I'm in that position the camera will not rotate over or under the ship, but in a circular motion will staying in the relatively same spot (camera will flip over).
I've spent a decent amount of time searching for the answer, and can't seem to find it. Can anyone help me out?
if (Input.GetKey ("left")) { //left
transform.RotateAround (playerObject.transform.position, Vector3.up, camRotateSpeed * Time.deltaTime);
}
if (Input.GetKey ("right")) { //right
transform.RotateAround (playerObject.transform.position, Vector3.down, camRotateSpeed * Time.deltaTime);
}
if (Input.GetKey ("up")) { //up
transform.RotateAround (playerObject.transform.localPosition, Vector3.left, camRotateSpeed * Time.deltaTime);
}
if (Input.GetKey ("down")) { //right
transform.RotateAround(playerObject.transform.localPosition, Vector3.right, camRotateSpeed * Time.deltaTime);
}
Answer by Irnal · Dec 14, 2016 at 01:14 AM
Hi there,
The issue in your code is that the Global axis vector.up or vector.down stays the same relatively to the camera, as it is the vertical axis but regarding vector.left and vector.right it is not. As your camera moves around your playerObject (in vector.left/right axis), it changes the relative angle it has with the Global vector.left/right axis, because it doesn't share the same 'left' and 'right' with the world space. If you've noticed, when you move up or down you can see that camera's Z rotation is changing too.
So what you need for your axis is to be camera's left/right axis and not the world's. Here is the modification:
if (Input.GetKey("left"))
{ //left
transform.RotateAround(playerObject.transform.position, Vector3.up, camRotateSpeed * Time.deltaTime);
}
if (Input.GetKey("right"))
{ //right
transform.RotateAround(playerObject.transform.position, Vector3.down, camRotateSpeed * Time.deltaTime);
}
if (Input.GetKey("up"))
{ //up
transform.RotateAround(playerObject.transform.localPosition, -transform.right, camRotateSpeed * Time.deltaTime); // Modified Line
}
if (Input.GetKey("down"))
{ //down
transform.RotateAround(playerObject.transform.localPosition, transform.right, camRotateSpeed * Time.deltaTime); // Modified Line
}
Note: I just noticed that there is not transform.left (or down) so you have to type a minus (-) on the transform.right as i did to have the opposite direction.
I hope this is what you were looking for.
That works great! Thank you for the info/detailed response.
Follow this Question
Related Questions
I want to make the player move in the same direction that the camera moves 0 Answers
Roll a ball camera rotation on Q and E 0 Answers
How can i move the player to the direction that the camera points? 2 Answers
getting Jittery movement on camera when player rotating and moving in same time 0 Answers
When attaching my custom camera script camera shakes when player starts to move fast. 1 Answer