- Home /
Camera following two objects
Hello, i need some help, i'm making a 2D fighting game and i need a camera that follows two game objects or two fighters, the two fighters have character controllers, an my script just calculates the midpoint of the two objects, but there's a lot of jittering, and i don't know why, or what to do to not have jittering,
this is my script, it's just for experimentation so it's not so good yet, but it works, it always gives me the midpoint that i want.
function Update () {
var dt : float ;
var a : float;
var b : float;
var c : float;
var d : float;
var h : float;
var g : float;
var x1 = GameObject.Find("object1").transform.position.x;
var y1 = GameObject.Find("object1").transform.position.y;
var x2 = GameObject.Find("object2").transform.position.x;
var y2 = GameObject.Find("object2").transform.position.y;
a = x2 -= x1;
b = y2 -= y1;
c = a/2;
d = b/2;
h = c + x1;
g = d + y1;
transform.position = Vector3(h, g,0);
}
So i want to get rid of the jittering, and have a smooth camera, Please i need some help!
Try using lerp : http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html
var delay : float = 0.2;
transform.position = Vector3.Lerp( transform.position, Vector3(h, g, 0), delay );
Also, only use GameObject.Find once in Start, store references to the gameObjects there.
var obj1 : Transform;
var obj2 : Transform;
function Start()
{
obj1 = GameObject.Find("object1").transform;
obj2 = GameObject.Find("object2").transform;
}
// in Update
var x1 = obj1.position.x;
var y1 = obj1.position.y;
var x2 = obj2.position.x;
var y2 = obj2.position.y;
Thanks a lot it worked very nice, if no gravity is used in the rigid body, it works great since character controller has a gravity variable, but i don't know if it's a good idea to not use gravity in the rigid body. Thanks anyway.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Camera follow help? 1 Answer
C# FP_Camera not following Character Controller 0 Answers
How to let a GameObject or Camera follow a path? 7 Answers
Smooth Follow Rotation 3 Answers