Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

I need help with a basic moving/jumping script

Discussion in 'Scripting' started by sheiro, Mar 6, 2015.

  1. sheiro

    sheiro

    Joined:
    Mar 6, 2015
    Posts:
    7
    Hi everyone,

    I`m new to scripting, and i tried to write my first movement script.
    It works fine, the only problem is that while my player is jumping or in-air, i still can control his movement, allthough i thought i disabled that.
    I used this Tutorial as a guide, but i don`t know if maybe the author did a mistake or i missed something:

    https://www.packtpub.com/books/content/unity-3x-scripting-character-controller-versus-rigidbody

    My code looks like that:

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. public var Speed : float = 5.0;
    4.  
    5. public var moveDirection : Vector3 = Vector3.zero;
    6.  
    7. public var jumpSpeed : float = 500.0;
    8.  
    9. public var jumpDirection : Vector3 = Vector3.zero;
    10.  
    11. public var isGrounded : boolean = false;
    12.  
    13. public var Jumping : boolean = false;
    14.  
    15. public var inAir : boolean = false;
    16.  
    17. public var airControl : float = 0.5;
    18.  
    19.  
    20.  
    21.  
    22. function Movement ()
    23.  
    24. {
    25.  
    26.     if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
    27.     {
    28.         moveDirection = Vector3(Input.GetAxisRaw("Horizontal"), moveDirection.y, Input.GetAxisRaw("Vertical"));
    29.    
    30.         this.transform.Translate((moveDirection.normalized * Speed) * Time.deltaTime);
    31.     }
    32.    
    33.     if (Input.GetButtonDown("Jump") && isGrounded)
    34.     {
    35.         Jumping = true;
    36.        
    37.         jumpDirection = moveDirection;
    38.        
    39.         GetComponent.<Rigidbody>().AddForce((transform.up) * jumpSpeed);
    40.     }  
    41.    
    42.     if (isGrounded)
    43.     {
    44.     this.transform.Translate ((moveDirection.normalized * Speed) * Time.deltaTime);
    45.     }
    46.    
    47.     else if (Jumping || inAir)
    48.     {
    49.     this.transform.Translate ((jumpDirection * Speed * airControl) * Time.deltaTime);
    50.     }
    51.  
    52. }
    53.  
    54.  
    55.  
    56. function Start () {
    57.  
    58. }
    59.  
    60. function FixedUpdate ()
    61. {
    62.     if (!isGrounded)
    63.     {
    64.         if (Physics.Raycast(transform.position, -transform.up, transform.localScale.y + 0.1))
    65.         {
    66.         isGrounded = true;
    67.         Jumping = false;
    68.         inAir = false;
    69.         }
    70.    
    71.         else if (!inAir)
    72.         {
    73.         inAir = true;
    74.         jumpDirection = moveDirection;
    75.         }
    76.     }
    77.  
    78. Movement();
    79.  
    80. }
    81.  
    82. function OnCollisionExit (collisionInfo : Collision)
    83.     {
    84.     isGrounded = false;
    85.     }
    86.  
    87.  
    88.  
    89.  
    90.  
    91.  
    I appreciate any help!
     
  2. codejunkie83

    codejunkie83

    Joined:
    Aug 23, 2013
    Posts:
    2
    In the first If statement, you still have the translate, which will happen regardless of if you are grounded or not.
     
  3. sheiro

    sheiro

    Joined:
    Mar 6, 2015
    Posts:
    7
    Thanks for the tip!
    But i still don`t know how to edit the code, so that the movement function works only when grounded.
    I tried this, but i think it is wrong and did not work:

    Code (JavaScript):
    1.     if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical") && isGrounded)
    2.     {
    3.         moveDirection = Vector3(Input.GetAxisRaw("Horizontal"), moveDirection.y, Input.GetAxisRaw("Vertical"));
    4.    
    5.         this.transform.Translate((moveDirection.normalized * Speed) * Time.deltaTime);
    6.     }
    I think i have to change something with the jumpDirection, as it is mentioned in the tutorial, but i can`t figure out what... :(
     
  4. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    what codejunkie83 told you is this:
    Code (JavaScript):
    1. if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
    2. {
    3.   moveDirection = Vector3(Input.GetAxisRaw("Horizontal"), moveDirection.y, Input.GetAxisRaw("Vertical"));
    4.   // you have to delete or comment this! it does the same as the next "translate".
    5.   //this.transform.Translate((moveDirection.normalized * Speed) * Time.deltaTime);
    6. }
    Then your code will work as you said you wanted. Give it a try!
     
  5. sheiro

    sheiro

    Joined:
    Mar 6, 2015
    Posts:
    7
    Thanks, i tried it as you said, but now the problem is, that the character does only jump straight along the y-axis, without caring for the movedirection.
    So when i move and than jump, the movement stops instantly and the character jumps upward only.
    My intention was, that when i move, and then jump, the character should "take" the velocity of the move and continue in that direction.
    Hope someone could help me with that.
     
  6. Ap0C552

    Ap0C552

    Joined:
    Feb 7, 2015
    Posts:
    43
    Well I think you answered your own question.

    You have to save X delta and Y delta and for as long as you are jumping modify you speed by that X and Y delta.
     
  7. sheiro

    sheiro

    Joined:
    Mar 6, 2015
    Posts:
    7
    I think i don`t understand what you mean with "delta", do you mean the vector of the movement?
    I changed the script a few times, but it still seems to be broken. Maybe i´m completley wrong with the way i do it :confused:
     
  8. PJRM

    PJRM

    Joined:
    Mar 4, 2013
    Posts:
    303
    sheiro, it means you have to save the last interaction of your controls.
    try changing this:
    Code (JavaScript):
    1. if (Input.GetAxis("Horizontal") || Input.GetAxis("Vertical"))
    to this:
    Code (JavaScript):
    1. if (Input.GetAxisRaw("Horizontal") || Input.GetAxisRaw("Vertical"))
    Since you moving your character with raw input, in my point of view you should get the raw controls either.

    My idea of solution: In this IF you should store the movement in a different variable (either) while it is grounded. so when
    Code (JavaScript):
    1. if (!IsGrounded) {
    2.   // you will translate the jump with this stored value.
    3. }
     
  9. sheiro

    sheiro

    Joined:
    Mar 6, 2015
    Posts:
    7
    I tried it but it still doesn`t work as i intended :(

    Thanks anyway !