- Home /
C# - Offset In-Air Camera from Current Object Position
Hey folks, pretty straight forward one here. I have a player character object on the ground, and an angled camera in the air. The camera doesn't need to rotate around or anything like that, all I'm wanting to do is have the camera offset from the player character position by a few units. I can't for the life of me remember how to actually reference the player object position inside the cameras transform.Translate vector3.
Can anyone help me out with this derpy one :P
Cheers! Matt
Edit:Basically on the camera, all I wanna do is Transform.position = new Vector3(0,0,PlayerPosition-10)
But I can't dig up the right syntax lol
Answer by aldonaletto · Dec 17, 2011 at 12:25 PM
You usually must have a target variable that references the target, and keep some predefined offset from it (camera script):
Transform target; // drag the target here (or find it at Start) Vector3 offset = new Vector3(0, 2, -3); // define the offset
void LateUpdate(){ transform.position = target.position + offset; // you can also force the camera to look at the target: transform.LookAt(target); } This is a very simple following camera script - a more elaborated one could follow the target with Vector3.Lerp in order to have a smoother movement, and possibly keep a constant angle from the target.
NOTE: You can find the target at Start with something like this:
void Start(){ target = GameObject.FindWithTag("Player").transform; }
Your answer
Follow this Question
Related Questions
Two vector3's interacting to make a postive number from two negatives. 1 Answer
C# 2d Spawn Randomly along Camera Borders 1 Answer
Camera & Parent getting destroyed on Play 0 Answers
How to move the player upwards and forwards at the same time? 1 Answer
Player Sprite Changing Scenes And getting a new camera to follow it and default spawn position 1 Answer