function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var back = transform.TransformDirection(Vector3.back);
var curSpeed = walkSpeed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if (Input.GetAxis("Horizontal") < 0.0){
animation.CrossFade("turn_left");}
if (Input.GetAxis("Horizontal") > 0.2){
animation.CrossFade("turn_right");}
if (Input.GetButton ("Run") && (Input.GetAxis("Vertical") < 0.0)){
animation.CrossFade ("turn_back");
walkSpeed = 0.0;
transform.Rotate = (Time.deltaTime, 3.725, 0);
Actually what I need is to get character turn back 180 degrees fast when buttons “Run” + “Down” (Vertical < 0.0) pressed and then stop in default (idle) pose even if still those buttons are pressed. I dont want to limit rotation angles when obviously rotate left or right, but only when Run + Down pressed. So what do I need to add to this script to make it work? Thanks
If you want to rotate 180 degrees quickly (but not instantly) when the key is pressed, maybe a coroutine is the best solution. Care must be taken to not call the coroutine again while it’s running, what can be done with the auxiliary boolean variable turningBack : it’s set when the coroutine starts and cleared when it ends, and the code in Update must be blocked while turningBack is true. There’s a pitfall however: rotating exactly 180 degrees is a problem because Unity may not identify about which axis you want to rotate (the object may rotate about very weird axes!) To avoid this problem, the coroutine splits the 180 degrees in two 90 degrees rotations done in sequence.
The code could be something like this:
var turnBackTime: float = 0.5; // duration of turn back
private turningBack = false; // true while turning back
function TurnBack() // coroutine
{
turningBack = true; // don't call me - I'm turning back!
var rot0 = transform.rotation; // initial rotation
var rot1 = rot0 * Quaternion.Euler(0, 90, 0); // first 90 degrees
var rot2 = rot1 * Quaternion.Euler(0, 90, 0); // last 90 degrees
var rate: float = 2 / turnBackTime; // calculate the rate for 2 90 degrees rotations
var t: float = 0; // control variable
while (t < 1){ // rotate first 90 degrees
t += rate * Time.deltaTime;
transform.rotation = Quaternion.Slerp(rot0, rot1, t);
yield;
}
t = 0; // do next 90 degrees rotation
while (t < 1){
t += rate * Time.deltaTime;
transform.rotation = Quaternion.Slerp(rot1, rot2, t);
yield;
}
turningBack = false; // rotation finished
}
function Update ()
{
// first change in Update: abort it while turning back
if (turningBack) return;
// the code below is the same as before:
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var back = transform.TransformDirection(Vector3.back);
var curSpeed = walkSpeed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if (Input.GetAxis("Horizontal") < 0.0){
animation.CrossFade("turn_left");
}
if (Input.GetAxis("Horizontal") > 0.2){
animation.CrossFade("turn_right");
}
// here's the second modification in Update:
if (Input.GetButtonDown("Run") && (Input.GetAxis("Vertical") < 0.0)){
animation.CrossFade ("turn_back");
walkSpeed = 0.0;
TurnBack(); // start the coroutine
}
...