- Home /
The question is answered, right answer was accepted
LookAt script Issues
Hi there.
I'm currently trying to setup up a camera rail system and have stumbled into a bit of a problem. I have a path for my camera to move along, and a target for my camera - however because my camera is using a LookAt script, once the game starts playing I dont seem to have any way of controlling the local tilt (or 'roll' i should say) of the camera. I dont suppose anyone could help me alter my LookAt script so as to 'release' my camera's ability to tilt.
Ideally I would like to have a separate public float (called Tilt perhaps) which I can use to alter the local z-rotation of my camera.
Below is my script - Any help is greatly appreciated:
using UnityEngine;
using System.Collections;
/*
* This is a modified C# version of Unity's standard SmoothLookAt script.
*
* The main benefit of this script is that it allows the user to not rotate an object on all of its axis to look at a target.
* For example, you might want a gun turret to swivel on the spot to face a player, but to not rotate the mesh 'vertically' if the player jumps.
* As a result the most common configuration with this script is to make the X-axis and Z-axis true, but to leave Y-axis false.
*
*/
public class LookAtAxisLock : MonoBehaviour
{
public bool Smooth = true;
public float damping = 6.0f;
public GameObject RotatingObject;
public GameObject LookTarget;
public bool X_axis = true;
public bool Y_axis = true;
public bool Z_axis = true;
private float X_position;
private float Y_position;
private float Z_position;
void Start ()
{
// Make the rigid body not change rotation
if (RotatingObject.rigidbody)
RotatingObject.rigidbody.freezeRotation = true;
}
void LateUpdate ()
{
//Update the lookTarget's current X,Y,Z locations
if (X_axis)
{
X_position = LookTarget.transform.position.x;
} else {
X_position = RotatingObject.transform.position.x;
}
if (Y_axis)
{
Y_position = LookTarget.transform.position.y;
} else {
Y_position = RotatingObject.transform.position.y;
}
if (Z_axis)
{
Z_position = LookTarget.transform.position.z;
} else {
Z_position = RotatingObject.transform.position.z;
}
if (Smooth)
{
// Look at and dampen the rotation
Vector3 relativePosition = (new Vector3(X_position, Y_position, Z_position) - RotatingObject.transform.position); // NB: For the Quaternion to work a vector must be created from the 'origin' (kindof)
Quaternion rotation = Quaternion.LookRotation(relativePosition);
RotatingObject.transform.rotation = Quaternion.Slerp(RotatingObject.transform.rotation, rotation, Time.deltaTime * damping);
}
else
{
// Just lookat
RotatingObject.transform.LookAt(new Vector3(X_position, Y_position, Z_position));
}
One simple solutions is to place the script above on an empty game object at the same position and rotation as the camera, make the camera a child of that empty game object, then use Transform.localRotation to 'roll' the camera.
Wow! But of course! Awww jeez.... its at times like this I totally feel like doing an 'epic palm to my face'. Cheers bud.