- Home /
Camera Target Object relative to Player Object
I have a top down 2d game and I want the CameraTarget object to always sit 1f away in the direction the user is facing.
Since I don't actually rotate my player to turn (i set anim floats based on input.getaxis) I can't just set it as a child and it gets done automatically.
So I base its position off the getaxis from the controller. This is ran on the CameraTarget object;
// get controller direction
Vector3 movement_vector = new Vector3 (Input.GetAxisRaw ("Horizontal"), Input.GetAxisRaw ("Vertical"), 0f);
// if controller is pushed in a direction...
if (movement_vector != Vector3.zero) {
// set camera to the player + controller direction
transform.position = (player.transform.position + movement_vector);
}
This works as a start, but it doesnt always sit 1f away in the direction the player last pushed on the controller.
Eg:
Any suggestions or ideas how I can adapt it to do that? I want it to move in a smooth arc if possible, but not sure of the maths involved.
Answer by Sajalsh25 · Sep 01, 2016 at 08:36 AM
if you are looking for the distance between the Camera and the player, i can give you a suggestion, i am currently learning unity too so pardon me if i am wrong here.
if you want to transform the camera position, make a camerafollow script and attach it to the camera. In the update() write Camera.main.transform.position = new Vector3 (X POSITION,Y POSITION,1f);
XPOSITION and YPOSITION depend on you!
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Detecting cross platform input? 0 Answers
Rotate camera on mouse drag, minor glitch 1 Answer
RTS Camera Rotation and Movement 0 Answers
GameObject and Camera 2 Answers