Hi i was wondering what am i doing wrong
i have a tanks’ gun and i can rotate it up/down but i want to restrict how mouch up or down you can rotate
float curRot = gun.rotation.z;
if( gun.rotation.z == Mathf.Clamp(curRot,0,17) ) {
//rotate
}
unfortunetly this doesn’t work … any suggestions?
transform.rotation isn’t represented in Euler angles, but Quaternions. For your examples you’d want to use gun.rotation.eulerAngles.z, but even THAT won’t work since you can’t modify single parameters of a Vector3. The correct approach would be to clamp curRot directly, then do gun.rotation.eulerAngles = new Vector3(gon.rotation.eulerAngles.x, gun.rotation.eurlerAngles.y, curRot).
i was goofing around with it and i got this monster
wasn’t sure how to use mathf.clamp effectyvly
if (rotateTouchPad.position.y > 0) {
if( (gun.rotation.eulerAngles.z < 17 gun.rotation.eulerAngles.z > 0) || (gun.rotation.eulerAngles.z < 360 gun.rotation.eulerAngles.z > 347) || gun.rotation.eulerAngles.z == 0 || gun.rotation.eulerAngles.z == 360 )
gun.Rotate (up);
}
if (rotateTouchPad.position.y < 0) {
if((gun.rotation.eulerAngles.z <= 17 gun.rotation.eulerAngles.z > 0) || (gun.rotation.eulerAngles.z <= 360 gun.rotation.eulerAngles.z > 347) || gun.rotation.eulerAngles.z == 0 || gun.rotation.eulerAngles.z == 360)
gun.Rotate (down);
}
the problem is when i go max up or max down i can’t go in diffrent direction … probably goes out of restricted area
up is vector 3 (0,0,1)
down is vector 3 (0,0,-1)
i tweaked it works
thanks for clearing that up for me
Mathf.clamp(a,b,c) essentially reads like this:
“If value a is between b and c, return it as is. If it goes outside of the b-c boundary, limit it to one of those.”