- Home /
 
Rotate on Key press help?
Hi everyone, thanks for opening this question. xD
So here's the deal. I've been trying to get this to work for weeks with no luck, I went through every related question I found here and googled all over, but couldn't get it to work.
I want to make a sidescroller game, and since I want physics too, I used RigidbodyFPSWalker from the Unify Script Wiki (assuming I need something like that).
Now the first problem arose when I imported the character (tried another too), it was facing a weird downward way, and after rotating it the way I want and applying the script to it the character isn't rotated correctly. And since I don't know how to rotate the object or controller individually from one another I managed to get the controls to work the way I want them by inverting and changing some stuff in the project input settings.
Now then I only mentioned all this cause I think it might interfere with the answer you guys give me. It now moves left (backwards), and right (forward) on key press and jumps. But I want to make it rotate 180 degrees left when pressed left and right when pressed right. Now I spent weeks trying to get this to work but without someone giving me the already done code I can't seem to do anything. I managed to rotate it 180 using this code but the controller rotates too not just the object:
 if (Input.GetKey (KeyCode.LeftArrow)) { transform.rotation = Quaternion.AngleAxis(180, Vector3(0,1,0)); }  And this is the RigidbodyFPSWalker I used:
 
                var speed = 10.0;
 var gravity = 10.0;
 var maxVelocityChange = 10.0;
 var canJump = true;
 var jumpHeight = 2.0;
 private var grounded = false;
  
 @script RequireComponent(Rigidbody, CapsuleCollider)
  
 function Awake ()
 {
     rigidbody.freezeRotation = true;
     rigidbody.useGravity = false;
 }
  
 function FixedUpdate ()
 {
     if (grounded)
     {
         // Calculate how fast we should be moving
         var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
         targetVelocity = transform.TransformDirection(targetVelocity);
         targetVelocity *= speed;
  
         // Apply a force that attempts to reach our target velocity
         var velocity = rigidbody.velocity;
         var velocityChange = (targetVelocity - velocity);
         velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
         velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
         velocityChange.y = 0;
         rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
  
         // Jump
         if (canJump && Input.GetButton("Jump"))
         {
             rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
         }
     }
  
     // We apply gravity manually for more tuning control
     rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
  
     grounded = false;
 }
  
 function OnCollisionStay ()
 {
     grounded = true;    
 }
  
 function CalculateJumpVerticalSpeed ()
 {
     // From the jump height and gravity we deduce the upwards speed 
     // for the character to reach at the apex.
     return Mathf.Sqrt(2 * jumpHeight * gravity);
 }
 
 
               PS: I apologize if I didn't post the codes the way I should. Its not working the way I wanted to either. xD 
 
              Answer by ChrisSch · Mar 18, 2013 at 01:43 AM
Never mind I solved all of those problems, first one in Blender it self and second with this code found in this question.
 function Update () {
     if (Input.GetButton("Horizontal")||Input.GetButton("Vertical")) {
        var newForward : Vector3 = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")).normalized;
        if (newForward!=Vector3.zero) transform.forward = newForward;
        transform.Translate(Vector3.forward*Time.deltaTime);
     }
 }
 
              Your answer