- Home /
Attaching a Camera to the Player
This is going to make me sound dumb but I am working on a side scroller and when I drag the camera to the player to attach it, the camera's z position is moved to the gameObject. I want to move the camera back off the object, but I can only adjust the x and y axes.
Answer by jaised · Feb 25, 2012 at 06:00 AM
I am not sure why you would want to attach the camera to the player. Is there something specific that you want to do with it? Because an easier way would be to attach a custom script much like the following:
#pragma strict
///
/// SmoothFollow:
/// Follows the target's position while applying damping to prevent jittering
/// and immediate moving of the camera.This way every subtle movement doesn't become
/// noticeable. The camera follows from a set distance away from the character and always
/// tries to center the view on the target.
///
// public fields
var target : Transform; // the target camera is following
var dampTime : float = 0.3; // offset from the viewport center to fix damping
var distance : float = 10; // distance away on z axis from the target
// private fields
private var _velocity = Vector3.zero; // velocity of camera relative to target
///
/// Update:
/// Makes sure that the camera is always focused on the target,
/// after applying damping.
///
function Update() {
if(target) {
// calculate the difference between camera and target positions
var delta : Vector3 = target.position - camera.ViewportToWorldPoint(Vector3(0.5, 0.5, distance));
// calculate destination of the camera based on the difference in positions
var destination : Vector3 = transform.position + delta;
// apply the new position through dampening to provide smooth movement
transform.position = Vector3.SmoothDamp(transform.position, destination, _velocity, dampTime);
}
} // eo Update
Then all you have to do is attach this script to your camera, and link your player Game Object to target and BAM! Good luck.
Answer by Kleptomaniac · Feb 25, 2012 at 10:27 AM
So I think what you're saying is that the camera is not giving you the scene depth you want? An approach to this could be to add a script to the camera called SideScrollCamPos:
var player : GameObject;
var depthZ : float; //Here you will want to specify the z distance of the camera from the character
function Update () {
transform.position = Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z - depthZ);
}
I think that should work, but with me there's always an element of being completely wrong ...
Good luck! Klep
Answer by cosmopod · Dec 24, 2013 at 08:17 PM
It's better if you use LateUpdate() for this purpose instead Update()
Your answer
Follow this Question
Related Questions
Attaching an object to a camera movement weird, Help! 2 Answers
How can I change camera when colliding with "X" object? 1 Answer
How to set the position of objects on the left and right of the screen 0 Answers
Typecasting Camera to Object for ObjectField 3 Answers
How to attach objects using a mouse click on one side, and object only attaches to that one side? 0 Answers