x


Smoothly following a moving object

I want the main camera to follow a spaceship. I attached the following code to the camera:

void Update ()
{
// target is the spaceship's transform
    Vector3 wantedPosition = target.TransformPoint (0, height, distance);
    transform.position = Vector3.Lerp (transform.position, 
wantedPosition, Time.deltaTime * damping);
    // ... code for rotation...
    }

However, as the target (my spaceship) moves forward, the camera shakes unpleasantly, I believe due to the Lerp function. Is there a way to avoid this and just have the camera move the same way as the spaceship does? Thanks.

more ▼

asked Dec 28, 2010 at 05:22 PM

avatar image

Silvia
40 15 10 16

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

You could use the 'built-in' script "Smooth Follow".

Attach the script (Component/Camera-Control/Smooth Follow) to your camera and adjust the values in the inspector.

more ▼

answered Dec 28, 2010 at 05:31 PM

avatar image

efge
5.4k 20 38 59

I tried to adjust the parameters, but still I always get the camera inside the spaceship (which is not what I wanted: I would like a third-person point of view) and the shaking effect. Is there something I am missing?

Dec 28, 2010 at 05:48 PM Silvia

IN which asset can we find the Smooth Follow script?

Nov 05, 2012 at 09:32 PM Karsnen_2
(comments are locked)
10|3000 characters needed characters left

For the 'shaking' problem, try moving the code you posted from Update() to FixedUpdate().

more ▼

answered Dec 28, 2010 at 07:57 PM

avatar image

Jesse Anders
7.8k 13 33 67

Mmh.. the problem is even worse now... Can it depend from my computer being not enough powerful?

Dec 29, 2010 at 12:01 PM Silvia
(comments are locked)
10|3000 characters needed characters left

You could just use this script that I wrote - it's not the fanciest of things but it gets the job done

I had the same problem with a spaceship shaking too

 var playerMarker : Transform;
 
 function LateUpdate () {
     transform.position = Vector3.Lerp(transform.position, playerMarker.position, Time.deltaTime * 100);
     transform.rotation = Quaternion.Lerp(transform.rotation, playerMarker.rotation, Time.deltaTime * 100);
 }
more ▼

answered May 17, 2012 at 03:45 PM

avatar image

Ben Ezard
274 21 19 24

(comments are locked)
10|3000 characters needed characters left

// A simple smooth follow camera, // that follows the targets forward direction

var target : Transform; var smooth = 0.3; var height = 0; var distance = 5.0; private var yVelocity = 0.0;

function Update () { // Damp angle from current y-angle towards target y-angle var yAngle : float = Mathf.SmoothDampAngle(transform.eulerAngles.y, target.eulerAngles.y, yVelocity, smooth); // Position at the target var position : Vector3 = target.position; // Then offset by distance behind the new angle position += Quaternion.Euler(0+height, yAngle, 0) * Vector3 (0, 0, -distance); // Apply the position transform.position = position;

 // Look at the target
 transform.LookAt(target);

}

more ▼

answered May 18, 2012 at 12:26 AM

avatar image

urawhat
26 3 2 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5298
x530
x396

asked: Dec 28, 2010 at 05:22 PM

Seen: 6881 times

Last Updated: Nov 05, 2012 at 09:32 PM