- Home /
How to clamp Lookat?
I have an object following another. As an example a security camera shouldn't be able to do a full 360 if the wall is in the way or a turret should be limited so it won't go past a certain angle and shoot it's team mates. I was hoping I could use something like this:
transform.LookAt(target); if(transform.rotation.x > 60){transform.rotation.x = 60;}I guess the best way to do this would be to use a euler rotation derived from the target
someAngle = Mathf.Clamp(someAngle, MinClamp, MaxClamp); transform.rotation = Quaternion.Euler(someAngle,0,0);I don't know how to get the someAngle from the target/origin positions which is why I was using lookAt since that's what it's for. Any help clamping the angle on one axis?
Answer by redteardrop · Aug 11, 2012 at 08:08 AM
I figured out how to do it and am posting in case anyone else needs it.
You'll need 3 things. 1. a target 2. the object you want to do the looking 3. a dummy object (just make sure your dummy is a child of your object so it'll share the parent rotation and position or you'll get some odd angles)
Set the dummy to look at the target, read the dummy rotation into a variable then clamp that variable and pass it onto the rotaion of the final object.
var dummyObject:Transform; var targetPoint:Transform; var tempAngle:float; function LateUpdate() { dummyObject.LookAt(targetPoint); if(Cannon3.transform.rotation.x != 0) //seem to get a 0 if my target is in //certain places because I am using a mouse pos and hit with plane. { tempAngle = Cannon3.rotation.x * 100; //don't know how to read a euler //angle into a float so I just multiplied by 100 to get the angle. } else { tempAngle = tempAngle; }
Cannon1.rotation = Quaternion.Euler(tempAngle,0,0);
}
Great solution. And remember everyone you can dynamically create game objects, just do like GameObject dummyObject = new GameObject(); dummyObject.name = "Dummy Object"; dummyObject.transform.parent.transform = this.gameObject.transform;
Answer by ScroodgeM · Aug 10, 2012 at 09:09 PM
sol 1
declare/create a basic quaternion that will be central rotation of object's 'white (where object can rotate only)' or 'black(where object can't rotate)' area on rotation calc look rotation and calc an angle between calculated and one from previous tick * if angle more then maximum (for white) or lower then minimum (for black) - just move away from black or toward white for an angle then was over limitsol 2
use vector3 projections to different axis to calc offsets and limit them if needed. for example, Vector3.Dot(....) returns cosine of angle between vectors muliplied with it's distances. so if you got negative value it means angle between them is more them 90 degrees. all you need now is declare some vectors that will used for this limits.creating some workflow visual gizmos will be perfect in this case
That may be understandable to some, but I haven't a clue to what you're talking about. I figured out a way to get it done. I think it's a combination of what you were talking about and my own solution.
i can explain if you point to place that is understandable.
I would probably ask for some coding example, but since I found a way to get it to work exactly as I need it I think I'll leave it at that. Thanks though. :-)
Your answer
Follow this Question
Related Questions
Make object only rotate in 90 degree increments 0 Answers
Constraining turret rotation in local space 1 Answer
How to clamp the rotation of this? 0 Answers
problems with quaternion rotations 4 Answers