I have a camera that is looking at a player object at a 45 degree angle on a key press. As of now the rotation around the player works great.
The problem lies when I try to add a smooth camera follow at that 45 degree angle, because as the rotation changes, the follow coords change, and it gets a little wonky. Any of the typical camera follow methods I have used seems to have failed, I believe there is some fancy math work here that is beyond me,that or I’m overlooking something simple… Any help would be hugely appreciated!
Here is the code for the camera as of now:
public GameObject targetObject;
private float targetAngle = 0;
const float rotationAmount = 1.5f;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
targetAngle -= 90.0f;
}
else if (Input.GetKeyDown(KeyCode.E))
{
targetAngle += 90.0f;
}
if (targetAngle != 0)
{
Rotate();
}
}
protected void Rotate()
{
if (targetAngle > 0)
{
transform.RotateAround(targetObject.transform.position, Vector3.up, -rotationAmount);
targetAngle -= rotationAmount;
}
else if (targetAngle < 0)
{
transform.RotateAround(targetObject.transform.position, Vector3.up, rotationAmount);
targetAngle += rotationAmount;
}
}
I like to move my camera to a given point in space, and make it look at another given point.
For both of those points I like to track their current position as well as their desired position.
I usually use blank GameObjects to store their locations, and then in code store their desired positions.
For instance, if the camera is behind the player and I tap a key and want it to view the player from the right, I would move the camera position GameObject around from behind to the side at a given speed, copying its position to the camera’s transform, and then after each frame of moving I would use the .LookAt() function on the Camera transform to keep it focused on the look point, generally the player.
Alternately if you are looking down on the player and want to bring the attention temporarily to something interesting, you would move the look point towards the interesting thing, each frame telling the camera to .LookAt() that point.
With this way there is no rotation or angles or anything to track, just “I’m here, I’m looking at that.” And “here” and “that” can move, and I will always stay with it. It’s just a lot simpler.
Dunno if you’ve considered it, but there’s also Cinemachine from Unity. I looked at it, it’s pretty spiffy, but I found it to be a lot heavier and awkward for most of my simple uses. It sorta sounds like your use is on the simpler end of things too, but I just wanted to bring it up for you.