How I do a static camera attach

Hi every one,

I have a problem, Im attach a camera to a tank.

I want to move the tank, but I dont want to do tremble the camera, if the tank climb a small hill, I want the camera still static.

I mean, the camera is attach to the cannon tank, ok, but I dont want that the camera follow each movement of the tank, only follow the tank and I use their rotation Up, Down, and 360 degrees (X,Y Axis) whith the Script MouseLook.

There is a form to do? Or I need to be more explicit?

Thank you in Advanced.

yup, what you want to do is known as a “chase cam”, a camera that follows the object of interest but is somewhat laggy in catching up to its moves.

What this generally means is that on every update the camera should move proportionally from where it is now to where it would have been if always firmly “locked” onto the tank.

For position, during your fixed update try something like this:

First, define a camera location you want when the tank is at rest, eg:

Vector3 camera_location = new Vector3(0,5,-10);

(this is 5 units above and 10 units behind the tank)

Vector3 target_position = tank.transform.position + tank.transform.TransformPoint(camera_location);

target_position is now where the camera is trying to get to, now just smoothly move the camera towards that point:

camera.transform.position = Vector3.Lerp(camera.transform.position, target_position, Time.fixedTime*zoom_factor);

(you can tune the zoom_factor from 1.0 to whatever to get the feel you want)

A similar approach for rotation also works well.

There’s a script in this thread that implements a physics-controlled camera that might be of use to you. It doesn’t rigidly follow the object, but gets “pulled” along by it gently and it has steadicam-type behaviour as regards tracking the ground.

Thank you, I will try.

But, I worry about the MouseLook Script, this will to generate problems?

I will try and post the result.

Thanks

Hi again,

Im found this Script.

// Smooth towards the height of the target
var target : Transform;
var smoothTime = 0.3;
private var yVelocity = 0.0;

function Update ()
{
var newPosition = Mathf.SmoothDamp(transform.position.y, target.position.y,
yVelocity, smoothTime);
transform.position.y = newPosition;
}

But when I compile appear a Error:

“Cannot modify the return value of `UnityEngine.Transform.position’ because it is not a variable”

and… what is wrong? I do it like the Unity Script Page do.

Thank you in advanced.

yeah, you’ve run into the #1 sucky thing about scripting in C#, that transform.position is a property or whatever and its elements aren’t directly modifiable.

You have to reassign the entire variable like so transform.position = new Vector3(blah blah blah);

nb; when you get an error the first thing you should do is search google for the error text. You never know what you’ll find, eg:

http://forum.unity3d.com/viewtopic.php?t=6900&highlight=properties

Oh, Thank you.

I will try, I changed the JS to C# code, and I forget this only thing. :slight_smile:

I will try again.