Search Unity

[Javascript] Character Controller + SmartJump!

Discussion in 'Scripting' started by static_cast, Jan 10, 2014.

  1. static_cast

    static_cast

    Joined:
    Aug 25, 2013
    Posts:
    25
    Before I begin, I would like to note that I'm not quite sure which forum to post this in, so I chose the most related one. ;)

    I have written a javascript "Smart Jump" that will jump a player to the block above it [with multi-jump capability]. I also included the character controller script along with it. Here it is!:

    Code (csharp):
    1.  
    2. //This game is block/tile-based, so each 40x40 sprite is changed with [Pixels to Units] so they are 1x1 unit.
    3. #pragma strict
    4. var speed = 0.05;
    5. var maxJump = 3;
    6. private var jump = 1;
    7.  
    8. function Start()
    9.  {
    10.  //Freeze rotation +fix z-rotation problem [player spawns and immediately collides, causing slight z rotation, but with my initiation script (spawn blocks according to position in array)]
    11.  transform.eulerAngles.z = 0;
    12.  rigidbody2D.fixedAngle = true;
    13.  }
    14.  
    15. function Update()
    16.  {
    17.  //Check for collision in all directions [fortunately not *that* FPS intensive]
    18.  var right : RaycastHit2D = Physics2D.Raycast(Vector2(transform.position.x+0.5,transform.position.y), Vector2.right,  0.00001);
    19.  var left  : RaycastHit2D = Physics2D.Raycast(Vector2(transform.position.x-0.5,transform.position.y), -Vector2.right, 0.00001);
    20.  var down  : RaycastHit2D = Physics2D.Raycast(Vector2(transform.position.x,transform.position.y-0.501), -Vector2.up,      0.1);
    21.  
    22.  //Get right-left input
    23.  if(Input.GetKey("d")  !right)
    24.   transform.Translate(Vector3( speed,0,0));
    25.  else if(Input.GetKey("a")  !left)
    26.   transform.Translate(Vector3(-speed,0,0));
    27.  
    28.  //Get jump input + check if exceeded maxJump
    29.  if(Input.GetKeyDown("w")  jump < maxJump)
    30.   {
    31.   jump++;
    32.   //Get the distance towards to nearest block
    33.   var distance = (Mathf.Round(transform.position.y+1) - transform.position.y)+0.05;
    34.  
    35.   //Reset y velocity
    36.   rigidbody2D.velocity.y = 0.0;
    37.   //Forumla for distance jumps
    38.   rigidbody2D.velocity.y += Mathf.Sqrt(distance * 2 * -Physics.gravity.y);
    39.   }
    40.  
    41.  //Reset jump count if grounded
    42.  if(down)
    43.   jump = 1;
    44.  }
    45.  
    46.  
    Thanks!

    |Edits->
    || 1: Slightly changed jump height +[0.01]
    || 2:Added raycast for checking if player grounded +changed raycast length
    || 3:Slightly changed maxjump code [starts at one now]
     
    Last edited: Jan 12, 2014
  2. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Thats brilliant and supersimple.

    Will use this for when I need some quick controls to test with.

    Thanks
     
  3. static_cast

    static_cast

    Joined:
    Aug 25, 2013
    Posts:
    25
    Hmm, it must not be in the right section. Anybody knows where it is supposed to go?