- Home /
Camera Height (y)
im working on a space shooter and bumped into a little problem... im using a script to control my camera movement but it always stays at the same height. i don't know how to add height to this script. i hope someone can help me with this problem.
here's the script im using:
var target : Transform; var distanceToFollow : float = 5.0; var height : float = 1.0; var moveSpeed : float = 1.0; var rotationSpeed : float = 0.2; var damping = 6.0; var smooth = true;
private var targetRay : Ray; @script AddComponentMenu("Camera-Control/Smooth Look At")
function Update () {
targetRay = new Ray(target.position,-target.forward);
point = targetRay.GetPoint(distanceToFollow);
point = Vector3.Slerp(transform.position, point, Time.deltaTime * moveSpeed);
transform.position = point;
transform.LookAt(target,target.up);
} function LateUpdate () { if (target) { if (smooth) { // Look at and dampen the rotation var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); } else { // Just lookat transform.LookAt(target); } } } function Start () { // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }
'
Answer by Henri 1 · Apr 05, 2011 at 08:04 AM
Or, you can use the Spring Follow camera, and set the applicable height and distance
That's the one where it sets desired camera position, desired look position and smooths them in? I've found that general idea to be the most versitile.
Answer by Owen-Reynolds · Apr 05, 2011 at 12:50 AM
Think about specifically where you want your camera to be and which way it should be looking.
These script comments may or may not be helpful:
// shoot a ray from the player (target.position) straight backwards
// (-target.forward is the same as target.backward):
targetRay = new Ray(target.position,-target.forward);
// set the camera position to be 5 away on that line:
point = targetRay.GetPoint(distanceToFollow);
If you adjust where you shoot from, then the camera will be somewhere else. For example, using target.position + target.right*3
will start the ray 3 to the player's right.