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.