Search Unity

Side-Scrolling Platformer Change Direction in Air

Discussion in 'Scripting' started by jordo834, Apr 15, 2011.

  1. jordo834

    jordo834

    Joined:
    Mar 20, 2011
    Posts:
    61
    Hi,
    I've been posting a lot of questions on here recently, but thankfully, finally, this is my last one, in almost all platformers, when the player is in mid air, by pressing left or right, the player can change direction and move either way, albeit at a slower speed... I would like to achieve that but am using the standard .move controller from the unity references, which is this code:
    Code (csharp):
    1.  
    2. /// This script moves the character controller forward
    3. /// and sideways based on the arrow keys.
    4. /// It also jumps when pressing space.
    5. /// Make sure to attach a character controller to the same game object.
    6. /// It is recommended that you make only one call to Move or SimpleMove per frame.    
    7.  
    8. var speed : float = 6.0;
    9. var jumpSpeed : float = 8.0;
    10. var gravity : float = 20.0;
    11.  
    12. private var moveDirection : Vector3 = Vector3.zero;
    13.  
    14. function Update() {
    15.     var controller : CharacterController = GetComponent(CharacterController);
    16.     if (controller.isGrounded) {
    17.         // We are grounded, so recalculate
    18.         // move direction directly from axes
    19.         moveDirection = Vector3(Input.GetAxis("Vertical"), 0,
    20.                                 Input.GetAxis("Horizontal"));
    21.         moveDirection = transform.TransformDirection(moveDirection);
    22.         moveDirection *= speed;
    23.        
    24.         if (Input.GetButton ("Jump")) {
    25.             moveDirection.y = jumpSpeed;
    26.         }
    27.     }
    28.  
    29.     // Apply gravity
    30.     moveDirection.y -= gravity * Time.deltaTime;
    31.    
    32.     // Move the controller
    33.     controller.Move(moveDirection * Time.deltaTime);
    34. }
    35.  
    Any help is greatly appreciated.
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Code (csharp):
    1.  
    2. if (controller.isGrounded) {
    3.         // We are grounded, so recalculate
    4.         // move direction directly from axes
    5.         moveDirection = Vector3(Input.GetAxis("Vertical"), 0,
    6.                                 Input.GetAxis("Horizontal"));
    7.         moveDirection = transform.TransformDirection(moveDirection);
    8.         moveDirection *= speed;
    9.        
    10.         if (Input.GetButton ("Jump")) {
    11.             moveDirection.y = jumpSpeed;
    12.         }
    13.  
    Change it so that you can still move left or right regardless if its grounded by moving the directional input outside like so:

    Code (csharp):
    1.  
    2. if (controller.isGrounded) {
    3.         // We are grounded, so recalculate
    4.         // move direction directly from axes
    5.  
    6. // snip
    7.  
    8.        
    9.         if (Input.GetButton ("Jump")) {
    10.             moveDirection.y = jumpSpeed;
    11.         }
    12.  
    13.  
    14. //moved here
    15.         moveDirection = Vector3(Input.GetAxis("Vertical"), 0,
    16.                                 Input.GetAxis("Horizontal"));
    17.         moveDirection = transform.TransformDirection(moveDirection);
    18.         moveDirection *= speed;
    19.  
    I would urge you to experiment by reading through it, and having a bit of trial and error to pick things up... would save you a headache in the future :)
     
  3. jordo834

    jordo834

    Joined:
    Mar 20, 2011
    Posts:
    61
    Thanks, i did hav a good look through it first and made a mess of the code twice :D so i thought i'd better take it to the experts....
     
  4. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I'm not an expert :p
     
  5. jordo834

    jordo834

    Joined:
    Mar 20, 2011
    Posts:
    61
    hey, i just put the code into the game, but now the character doesn't jump, i'm going to experiment with it but if anyone could help, i'd appreciate it very much
     
  6. Esperento

    Esperento

    Joined:
    Jul 24, 2011
    Posts:
    7
    I'm having the same issue. Did you manage to find a solution Jordo?
     
  7. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    213
    Make sure you still have the line that adds gravity...

    // Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;