- Home /
make transform.lookat go fast
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);
}
Your question doesn't make sense. The effect of transform.LookAt() is already immediate. I'm not sure you're using the word degrees correctly. A degree describes rotation.
Oh I though it was a measurement of temperature. No I mean degrees. If a cube rotates 2 degrees, I want the other object to rotate 200 degrees. Whenever the object this is looking at moves, this should look at it, but whatever it rotated by to keep looking at the object, I want that to be multiplied
the LookAt() method will not work in your case then. You need a custom LookAt that will add however much amplification you would like, so basically not a "LookAt" but a "RotateThis$$anonymous$$uchIfThisRotatesThis$$anonymous$$uch" kind of method. And the sarcastic attitude (although funny haha) won't help you get many answers here. But yeah you need to take the angle between the two objects (I think?) and then depending on that angle, have another object rotate to a certain angle. There is no clear answer to this question really, because it depends how your implementing all this stuff.
Answer by robertbu · Nov 10, 2014 at 09:21 PM
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.
Your answer
Follow this Question
Related Questions
Mega Bug In Turret - Help 5 Answers
rotate on the Y axis 1 Answer
Talking to multiple NPC's 2 Answers
Shooting in the LookAt direction 1 Answer
Top-Down LookAt problem 1 Answer