- Home /
Question by
RandomUser123 · Aug 23, 2016 at 10:41 PM ·
camerarotateplatformer
Rotate camera with player
I'm using the MouseOrbitImproved script and it more or less does what I want it to do but I need help implementing one more thing.
When the player runs to the right or left, I would like the camera to follow him around in a circle, similar to in most platform games.
What's the best way to achieve this? Here's the Mouse Orbit code
void LateUpdate()
{
if (target)
{
x += Input.GetAxis("Mouse X") * xSpeed * distance * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
RaycastHit hit;
if (Physics.Linecast(target.position, transform.position, out hit))
{
distance -= hit.distance;
}
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse Y") * 0.1f, distanceMin, distanceMax);
Quaternion rotation = Quaternion.Euler(y, x, 0);
Vector3 negDistance = new Vector3(0.0f, 0.0f, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
Comment
What do you mean by following the player around in a circle? I don't remember that being a feature in platformers
Answer by alexander11 · Aug 23, 2016 at 11:36 PM
Set the camera as a child object to the Object you use as the Player.