- Home /
Keep Camera from player at a certain distance while orbiting around player
So far I have everything working perfectly with the following code. The problem is, using transform.forward while keeping distance sets the camera's Y-axis to the same height as the player's Y-axis. I want the camera to be a bit higher than the player rather than equal to the player.
The reason I am not simply attaching the camera to the player is because the player is a sphere that is constantly rotating. The method I am using keep the camera separate from the player while still keeping its distance. The player's movement is based on the direction the camera is facing, if that matters at all.
using UnityEngine; using System.Collections;
public class RotateCamera : MonoBehaviour
{
public float rotateSpeed = 300.0f;
public float distance = 3.0f;
public GameObject player;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
// If A key is pressed, rotate camera to the left
// If D key is pressed, rotate camera to the right
// if S key is pressed, rotate the camera 180 degrees around player
if (Input.GetKey(KeyCode.A))
{
transform.RotateAround(player.transform.position, Vector3.down, rotateSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.D))
{
transform.RotateAround(player.transform.position, Vector3.up, rotateSpeed * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, transform.eulerAngles + 180f * Vector3.up, 100 * Time.deltaTime);
}
}
void LateUpdate ()
{
// Keep camera a specified distance away from player
transform.position = player.transform.position - transform.forward * distance;
}
}
EDIT: basically I want to combine these two lines of code:
transform.position = player.transform.position - transform.forward * distance;
Combined with
transform.position = player.transform.position - transform.up * distance;
Answer by NickvanDokkum · Dec 11, 2014 at 08:23 AM
okay, so, have you tried to having a gameobject in the player that dosn't rotate, then putting the camera in that?
If I parent an empty object to the player and set it to a certain height with locked rotation, then the camera doesn't rotate either. I have not tired having an unparanted object that follows the player by script and having the camera follow that empty object. It seems really inefficient though so I was hoping there were other solutions
Answer by taxvi · Dec 11, 2014 at 08:47 AM
you should tilt the camera down at the desired angle before hitting Play button
That's a good idea. I'll try it out. However I think either the code will either tilt the camera back as well or the camera will be pointing at the ground, which isn't that I'm looking for
if this does not work, try calling the transform.LookAt() on line 36, it's very easy to use
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
MouseWheel Lerp Smoothing Problem 1 Answer
Constrain axis of isometric camera ("vertical" only) 0 Answers
Z axis change 2 Answers