- Home /
Help clamping a Rotation
I know this has been asked before, but, I can´t implement the other answers within my code (very noob here). Any help, greatly apreciated:
I have a turret and want to clamp the rotation, at 90° an 0°, so that it can only fire within that range.
var angle:float = 0.0f;
function Tilt (){
var spinAmount = Time.deltaTime*100;
if (Input.GetKey(KeyCode.Q))
{
angle = spinAmount;
transform.Rotate(angle,0,0);
}
thankx!
Can I see what the turret looks like in-game? Is the whole thing rotating or just the turret head etc?
Hi xortrox, thanks for your interest:)
Is is actually an empty object that i have it attached to my character ( a simple sphere) from where im generating the cannonballs.i figure this would make things simpler and more efective.
You may want to make sure that the initial angle can go from 0 to 90 in your case without the euler angles exceeding above 360 or under 0, then you could use $$anonymous$$athf.Clamp:
angle = spinAmount;
transform.Rotate(angle,0,0);
Vector3 cannonAngles = transform.eulerAngles;
cannonAngle.x = $$anonymous$$athf.Clamp(cannonAngle.x, 0, 90);
transform.eulerAngles = cannonAngles;
This is only one of many ways to do it, you could also be using Quaternions
Answer by ToxxicSin · Apr 23, 2014 at 03:39 AM
The simplest way in my opinion is to do Mathf.Clamp.
funtion Update(){
var anglesToEdit : type;
angleToEdit.angle = Mathf.Clamp(angleToEdit.angle), -45, 45);
//the angle you're clamping is probably the .y
//because that is the vertical axis which is used
//to rotate left and right
}
and personally I would clamp the angle between -45 and 45 because your turret more than likely originally spawns with the center facing forward at 0
Clamping euler angles from -45 to 45 will get troublesome because euler angles are simply horrible in comparison to quaternions when it comes to clamping, you most likely have to make sure your rotation never exceeds 360 degrees or goes below 0 to clamp a euler angle.
@xortrox Can't you do a $$anonymous$$imum of 315 and a maximum of 45?
Try your code with clamping -45 to 45 do a quick setup in unity you'll notice it probably wont work and most likely only uses 0-45 degrees as euler angles dont go below 0 degrees You can however control the angle yourself and have a "starting" rotation like this:
float angleOffset = 0.0f; float startAngle = 0.0f;
void Update() {
angleOffset += Input.GetAxis("$$anonymous$$ouse X"); $$anonymous$$athf.Clamp(angleOffset,-45,45);
Vector3 v = transform.eulerAngles; v.x = angleOffset; //or v.x = $$anonymous$$athf.Lerp(v.x, angleOffset, Time.deltaTime);
transform.eulerAngles = v;
}
I'm not sure if this would work well at all
Behold my glorious illustration,
In this example the 315 to 45 rotation involves a much larger range than my 0 to 90 degrees rotation, yet you would probably expect -45 to 45 to be the same as the 0 to 90 rotation, this is how euler angles would interpret it
If 315 is the $$anonymous$$imum, I thought the angle would be -45 to 45. If 315 was the maximum then your explanation would make sense, but I was unaware it would still interpret it like that.
Your answer
Follow this Question
Related Questions
How to clamp Lookat? 2 Answers
Turret rotation clamp. 2 Answers
Clamping a wrapping rotation. 6 Answers