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. Dismiss Notice

How Do I Make a Fast-Fall Script?

Discussion in '2D' started by Pixelkitty, Jan 11, 2015.

  1. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Okay, my other problem is solved, by myself, I might add. Yay! Now, I've been trying to make a fast-fall system, as is implemented in some fighter games (like Super Smash Bros). For some reason, I can crouch while in the air and do not fall faster. I know there's a lot in this script but it's the player-character script.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     public float maxSpeed = 10f;
    7.     bool facingRight = true;
    8.    
    9.     Animator anim;
    10.  
    11.     public float jumpForce = 50f;
    12.     public float normalfall = 2f;
    13.     public float fastfall = 9f;
    14.     public bool grounded;
    15.    
    16.     void Start ()
    17.     {
    18.         anim = GetComponent<Animator>();
    19.        
    20.         this.rigidbody2D.gravityScale = normalfall;
    21.     }
    22.    
    23.     void  Update ()
    24.     {  
    25.  
    26.     }
    27.    
    28.     void FixedUpdate ()
    29.     {
    30.         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
    31.        
    32.         if (grounded = true)
    33.         {
    34.             if (Input.GetButton ("Down"))
    35.             anim.SetBool ("Crouch", true);
    36.  
    37.             if (Input.GetButtonUp ("Down"))
    38.             anim.SetBool ("Crouch", false);
    39.         }
    40.         else if (grounded = false) //This does not work
    41.         {
    42.             if (Input.GetButton ("Down"))
    43.             this.rigidbody2D.gravityScale = fastfall;
    44.  
    45.             if (Input.GetButtonUp ("Down"))
    46.                 this.rigidbody2D.gravityScale = normalfall;
    47.         }
    48.        
    49.         float move = Input.GetAxis ("Horizontal");
    50.        
    51.         anim.SetFloat ("Speed", Mathf.Abs (move));
    52.        
    53.         if (move > 0 && !facingRight)
    54.             Flip ();
    55.         else if (move < 0 && facingRight)
    56.             Flip ();
    57.  
    58.         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
    59.  
    60.         if (Input.GetButton ("Jump"))
    61.         {
    62.             grounded = false;
    63.             anim.SetBool ("grounded", false);
    64.         }
    65.     }
    66.    
    67.     void OnCollisionStay2D ()
    68.     {
    69.         grounded = true;
    70.         anim.SetBool ("grounded", true);
    71.  
    72.         if (grounded && Input.GetButtonDown ("Jump"))
    73.         {
    74.             rigidbody2D.AddForce(new Vector2(0, jumpForce));
    75.         }
    76.     }
    77.    
    78.     void Flip()
    79.     {
    80.         facingRight = !facingRight;
    81.         Vector3 theScale = transform.localScale;
    82.         theScale.x *= -1;
    83.         transform.localScale = theScale;
    84.     }
    85. }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Surely the:
    Code (CSharp):
    1.  else if (grounded = false) //This does not work
    should read:
    Code (CSharp):
    1.  else if (grounded == false) //This does not work
    Usually OnCollisionStay2D is a bit flakey, so you probably want to use a raycast to check whether you are grounded or not. You do it more or less like this:
    Code (CSharp):
    1.  
    2. bool grounded = Physics2D.Raycast(raycast.position,
    3.  -Vector2.up, .1f, 1 << LayerMask.NameToLayer("Floor"));
    4.  
     
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Oh, I forgot, GetButtonUp and GetButtonDown don't work as intended in FixedUpdate(). They should only be used in Update().
     
  4. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Well, that is definitely news to me! I'll switch 'em over.
     
  5. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    HOWEVER: I now can't seem to make the fast-fall gravity relent upon releasing the "Down" key. Any ideas?

    Code (CSharp):
    1. void Update ()
    2. {
    3.     if (grounded)
    4.     {
    5.         if (Input.GetButton ("Down"))
    6.         anim.SetBool ("Crouch", true);
    7.        
    8.         if (Input.GetButtonUp ("Down"))
    9.         anim.SetBool ("Crouch", false);
    10.     }
    11.    
    12.     else
    13.     {
    14.         if (Input.GetButton ("Down"))
    15.         this.rigidbody2D.gravityScale = fastfall;
    16.        
    17.         if (Input.GetButtonUp ("Down"))
    18.         this.rigidbody2D.gravityScale = normalfall;
    19.     }
    20. }
     
  6. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Can't really see anything wrong with your code. You might want to change it to this, but it really shouldn't matter:
    Code (CSharp):
    1.         if (Input.GetButton ("Down"))
    2.             this.rigidbody2D.gravityScale = fastfall;
    3.        else
    4.             this.rigidbody2D.gravityScale = normalfall;
     
  7. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    I think my keyboard is just relentlessly horrible. Thanks, though!
     
  8. Atifraza

    Atifraza

    Joined:
    Jan 13, 2015
    Posts:
    1
    first,you may have noticed that, the state clip starts a little offset from zero.Why was that?

    secondly,the animation clip's length can't match with the length of the clip in animation state.You can see the picture I attach.In fact the animation clip last for 6 frames,but 9 frames in mecanim state.Why was that?

    There will be some mistakes,when Im using codes to control the time precisely.For example,there is 16frames for me to input a key to change into fly state,when the jump state start.Then I set the end of the transition to about 16frames in the preview.If I input the fly key at the very end of the transition,say 15frame, there will be a fly trigger,but no fly animation,which means the animation time is not the same as the game time?