I made this script for my Beach Defender Game. It was to keep the player from rotating more than I want his to, because I'm a mean person like that. Any way it took me awhile to figure out how to not make them turn to far to the left, right, up, down.. You know. Here's the script. Pretty simple. Put under Update function. Feel free to suggest different ways. Or ask any questions.
var upDown = Input.GetAxis("Vertical") *Time.deltaTime * (rotateSpeed-10);
var leftRight = Input.GetAxis("Horizontal") * Time.deltaTime * (rotateSpeed-10);
//making it turn up or down
if(transform.localEulerAngles.x < 25 ){
transform.Rotate(upDown, 0, 0);
} else if (transform.localEulerAngles.x > 295){
transform.Rotate(upDown, 0, 0);
}
//making it turn left or right
if(transform.localEulerAngles.y < 65 ){
transform.Rotate(0, -leftRight, 0);
} else if (transform.localEulerAngles.y > 295){
transform.Rotate(0, -leftRight, 0);
}
//making it not exceed rotation limit (left and right) and preventing it lock up
if(transform.localEulerAngles.y >= 65 && transform.localEulerAngles.y < 295){
if(transform.localEulerAngles.y < 180){
transform.localEulerAngles.y = 64.9;
} else{
transform.localEulerAngles.y = 295.1;
}
}
//making it not exeed rotation limit (up and down) and preventing it lock up
if(transform.localEulerAngles.x >= 25 && transform.localEulerAngles.x < 295){
if(transform.localEulerAngles.x < 180){
transform.localEulerAngles.x = 24.9;
} else{
transform.localEulerAngles.x = 295.1;
}
}
//making sure it doesn't turn on it's z axis
if(transform.localEulerAngles.z != 0){
transform.localEulerAngles.z = 0;
}
really helped me for a scene I was working on.
ReplyDeleteThanks!
setting rotation of image with arrow keys
ReplyDelete