- Home /
Objects rotate but remain on the original axis...
I'm trying to rotate a capsule with A and D and then move it forward and back with W and S ... Simple stuff... For some reason my capsule remains on the set Z and X axis even though it's visibly turning in the edit window, and I can see the surface of it rotating in the Game window too. Why is it not moving down it's new heading?? using UnityEngine; using System.Collections;
 public class PlayerMovementDon: MonoBehaviour {
 
     public int topSpeed = 5;
     public int acceleration =150;
     public bool active = false;
     // Use this for initialization
     void Start () {
     }
     // Update is called once per frame
     void Update () {
         if (active) {
             // JUMP
             if (Input.GetKeyDown (KeyCode.Space)) {
                 rigidbody.AddForce (Vector3.up * 1500);
             }
             // FORWARD
             if (Input.GetKey (KeyCode.W) && rigidbody.velocity.z <= topSpeed) {
                 rigidbody.AddForce (0, 0, acceleration);
             }
             // BACKWARD
             if (Input.GetKey (KeyCode.S) && rigidbody.velocity.z >= -topSpeed) {
                 rigidbody.AddForce (0, 0, -acceleration);
             }
             // TURN LEFT
             if (Input.GetKey (KeyCode.A)) {
                 transform.Rotate(0,5,0);
             }
             // TURN RIGHT
             if (Input.GetKey (KeyCode.D)) {
                 transform.Rotate(0,-5,0);            }
         }
     }
 }
Works a treat, they now head along the correct direction. However, i now can't check the velocity correctly and limit its movement once it's turned. Is there a similarly simple 'relative' command for an objects velocity?
Sorry, didn't notice that first time around. I may be mistaken, didn't test and I'm a little rusty.
float forwardVelocity = transform.InverseTransformDirection(rigidbody.velocity).z;
No problem! You'd be surprised how quickly you'll pick up on how to approach the many various math problems you encounter program$$anonymous$$g for game design. Everybody gets a crash-course when they first start out. :)
Your answer
 
 
             Follow this Question
Related Questions
How to make the npc face the player. 2 Answers
Face to direction, in a Mario Galaxy style game? 1 Answer
Rotating character based on joystick angle, at an angle 0 Answers
Move and Rotate on a fixed axis? 1 Answer
Weird GetAxis behavior 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                