Hello, here is the Unity Docs script to make an object look at an object. However, I want to take this further and make this object look at something very sensitively. For example if my cube moved on its x-axis 2 degrees, I want this object to move on its x-axis 200 degrees. What can I do?
#pragma strict
var Cube : Transform;
// Rotate the camera every frame so it keeps looking at the target
function Update() {
transform.LookAt(Cube);
}
With rotations, it is easy to get what you ask for, and for it to not be what you really need. So here is some code that implements what you asked for:
#pragma strict
public var target : Transform;
public var magnifier : float = 5.0;
private var tracker : Transform;
function Start() {
tracker = new GameObject().transform;
tracker.LookAt(target);
transform.LookAt(target);
}
function Update() {
tracker.position = transform.position;
var dir = target.position - tracker.position;
var qLook = Quaternion.LookRotation(dir);
var qDelta = qLook * Quaternion.Inverse(tracker.rotation);
var angle : float;
var axis : Vector3;
qDelta.ToAngleAxis(angle, axis);
transform.rotation = Quaternion.AngleAxis(angle * magnifier, axis) * transform.rotation;
tracker.LookAt(target);
}
How it works: It uses an empty game object to track the target. It figure out the delta rotation necessary to look at the target. It converts the result to an angle/axis representation. Then it does an AngleAxis() rotation multiplying the ‘angle’ by a factor.