- Home /
Camera following an object...
How can I use parenting (or scripting) to allow a camera to follow an object along the x axis only?
Answer by 3dDude · Feb 20, 2011 at 02:18 AM
Well. if you want the camera to follow the object make the camera a child of the object, and position/rotate it to look at the object..
If you want a smooth version in script do something like this:
var target : Transform; var smooth : float = 5; function Update () { transform.position.x = Mathf.Lerp(transform.position.x,target.position.x,Time.deltaTime*smooth); transform.position.y = Mathf.Lerp(transform.position.y,target.position.y,Time.deltaTime*smooth);
}
Attach that script to the camera and set target to be the object the camera follows.
you can remove the line where it says transform.position.y == ect to stop it following the y axis...
Hope this helps!
Hello 3dDude.
Thanks for the code, it works very well. But when I attach this script to the camera and the object is moving fast, it appears to shake and flick.
I tried other ways to do it, including iTween, with no luck.
Hi there, is it possible to say the camera should be 1 unit behind the target on a certain axis? and could maybe someone translate this in to C# pls
Answer by Kazemc · Oct 13, 2012 at 03:32 PM
Hey! I have a simple code that can help you. Is very simple:
#pragma strict
var myCamera : Transform;
var target : Transform;
function Update ()
{
myCamera.position = target.position + Vector3(0, 10, 0);
}
Then you have to attach it to whatever gameObject and assign myCamera to your camera and target to your target ^^
Your answer
Follow this Question
Related Questions
Move the camera to the left when the mousecursor is left 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
How do I add a camera bounce effect every time the player lands? 0 Answers
Slow camera move speed? 2 Answers
Orient GameObject rotation to rotation of Main Camera 0 Answers