- Home /
[Solved] Help on a rotation issue for a Top-Down view?
Hello Devs,
I developing a Top-Down game where there are two different camera modes.
Camera follows the Player on a Top-Down view basis.
The camera suddenly stops moving and just looks at the player from it's position.
Now there might be a lot of transition between the two modes. During both the modes, I would not like the player not to get away from the screen AND I would like to have a lot of fluidity between those transitions.
On a Top-Down view the most common eulerangles is (90, 0, 0).
CODE SETUP :
I have a switch case to switch between those two modes, whenever needed.
Each of the two cases, have two common variable, which is "Target" position/rotation.
I have two common function for both of the cases, which lerps the postion and rotation from "this.transform"position/rotation to the "Target" position/rotation.
As the player has to be within screen at all times, I use a funtion `Quaternion.LookRotation(PlayerPosition - this.transform.position);`
CODE :
void Update ()
{
DeltaTime = Time.deltaTime;
switch (CurrentMode)
{
case CameraMode.FollowPlayer :
TargetPosition = new Vector3(PlayerPosition.x, (PlayerPosition.y + HeightFromTarget), PlayerPosition.z);
TargetRotation = RequiredRotation();
break;
case CameraMode.LookAtPlayer :
TargetPosition = this.transform.position;
TargetRotation = RequiredRotation();
break;
default:
break;
}
this.transform.position = Vector3.Lerp(this.transform.position, TargetPosition, DeltaTime/2);
this.transform.rotation = Quaternion.Lerp(this.transform.rotation, TargetRotation, DeltaTime * 50);
}
Quaternion RequiredRotation()
{
RotationalVector = (PlayerPosition - this.transform.position);
return Quaternion.LookRotation(RotationalVector);
}
PROBLEM:
The rotation seems to work fine, except for the mode where the camera is on the Top-Down view following the player. It gets a little messy as the camera suffers rotation on the Y axis which just confuses and makes things very complicated. I would like to find a solution to that.
SCREENSHOT:
During one frame on Top-Down view :
During another frame on Top-Down View :
During the camera being stationary and looking at the player :
CRITERIA :
I have to use the common Lerp function which could be found at the end of the Update function.
I can't manually use the function Quaternion.Euler(90, 0, 0) for Top-Down view.
The most common need is that, the camera should face the player at all times and not suffer a confusing Y rotation during the Top-Down view.
I would appreciate your help, as always. Thank you.
Answer by Bunny83 · Dec 11, 2012 at 12:28 AM
Well, the answer why this is happening is quite simple:
LookRotation and Transform.LookAt require more than a direction / position to look along / at in order to create a rotation. You didn't specified the upvector, so it defaults to "world up" (simply Vector3.up). That means LookRotation will make the z axis to point exaxtly along your direction vector, but the rotation around this vector is determined by the up vector.
Usually the camera should have up == world up, but if you go into the extreme and tip over, the camera would be upside down, that's why the rotation suddenly turns around to still have the upvector pointing upwards. You can "fix" the rotation by providing your own up vector which of course doesn't fix point upwards. How you calculate it is up to you. There's no general way to do this because it depends on what behaviour you want.
Well it did work. Thanks. I totally forgot the second paramater on LookRotation function. I kind supplied the normalized difference vector between the player and the camera. It worked.