Hey folks. Sorry if this has been asked and answered already. I’ve been digging and digging for the past 2 days and haven’t found anything that solves my problem. Maybe I’m not using the right search criteria. Please point me in the right direction if there’s already a thread on this. My problem is that I have my character controller set up so that it is rotating based on my vertical (“Up Down”) and horizontal (“Left Right”) inputs and uses variable I’ve set in Mecanim to drive the direction of the rotation. The problem is I don’t know how to make the rotation stop once the character is facing the right way. So if the character is facing “up” on the screen and I press the “left” key I want the player to rotate left until he’s facing left and then stop instead of continually rotate as I press “left” to keep him moving left. Hope that makes sense. Here’s the code snippet (I realize it’s probably sloppy so forgive my noob coding skills):
Sorry, it seems I had misunderstood your question when I first tried to answer, hopefully this update will be of more use to you.
As I don’t have the same ‘Up Down’/‘Left Right’ axis as you set up I have assumed that they act similarly to the predefined ‘Vertical’/‘Horizontal’ axis respectively in that they range between -1 and 1.
var rotationSpeed : float = 90.0; //the degrees/second of the rotation
var horiRotation : float; //the outputs from the input axis
var vertRotation : float;
private var rotAngle : float;
private var haveInput : boolean = false;
private var targetAngle : float;
function Update () {
vertRotation = Input.GetAxisRaw ("Vertical");
horiRotation = Input.GetAxisRaw ("Horizontal");
if(Mathf.Abs(horiRotation)+Mathf.Abs(vertRotation)>0){
haveInput = true;
}else{
haveInput = false;
}
if(haveInput){
targetAngle = (Mathf.Atan2(horiRotation, vertRotation)*Mathf.Rad2Deg);
rotAngle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetAngle, rotationSpeed*Time.deltaTime);
transform.eulerAngles = Vector3(transform.eulerAngles.x, rotAngle, transform.eulerAngles.z);
}
}
let me know if this is at least closer to an answer of your question or if you find any problems!
Good luck!
Old answer
maybe this will work for you:
private var startAngle : float;
private var targetAngle : float;
private var startTime : float;
var smooth : float = 1.0; // The number of seconds taken to rotate
private var angle : float;
function Update () {
if(Input.GetKeyDown(KeyCode.UpArrow)){
startTime = Time.time;
startAngle = angle;
targetAngle = 0;
}else if(Input.GetKeyDown(KeyCode.DownArrow)){
startTime = Time.time;
startAngle = angle;
targetAngle = 180;
}else if(Input.GetKeyDown(KeyCode.RightArrow)){
startTime = Time.time;
startAngle = angle;
targetAngle = 90;
}else if(Input.GetKeyDown(KeyCode.LeftArrow)){
startTime = Time.time;
startAngle = angle;
targetAngle = 270;
}
angle = Mathf.LerpAngle(startAngle, targetAngle, (Time.time - startTime)/smooth);
transform.eulerAngles = Vector3(0, angle, 0);
}
let me know if you don’t understand parts of it, or if something doesn’t work
The major difference is that I set the ‘transform.eulerAngles’ rather than using ‘transform.Rotate’
Thank you for taking the time to throw a solution my way. It works out really well except that I need to be able to stop the rotation anywhere in between the angle limits. With this method, the player does rotate correctly (unless I’m holding down any combination of movement keys and then it gets a little funky) but he won’t stop mid-rotation which is something I need because the character has to be able to aim at any disired angle. I just need it not to go past the angle the player is turning toward. Again, thanks! You have me pointed in the right direction (pun intended) and hopefully I can figure out the next step.