- Home /
 
2D (With Planes) Jump Script
I am working on a 2D platformer-type game. To achieve proper 2D, I have been using planes for characters, and then a slightly modified version of THIS script. I have got basic movement working with transform.Translate() , but I just can't achieve jumping!
This is my script for my player:
 var uvAnimationTileX = 24; 
 
                var uvAnimationTileY = 1;                      
 
               var framesPerSecond = 10.0;
 
               var jumpSpeed : float = 10;
 
               var textures : Texture2D[];
 
               /* textures:
 
                1 = running right
 2 = idle
 3 = running left
 
               */
 
               function Start() {
 
               }
 
               function Update () {
 
                if(Input.GetKey(KeyCode.RightArrow))
 {
     ChangeAnimation(0, 3, 10);
     transform.Translate(Vector3.left * 4);
 }
 else if(Input.GetKey(KeyCode.LeftArrow))
 {
     ChangeAnimation(2, 3, 10);
     transform.Translate(Vector3.right * 4);
 }
 else
 {
     ChangeAnimation(1, 4, 7);
 }
 if(Input.GetKeyDown(KeyCode.Space))
 {   
     Jump();
 }
 if(Input.GetKeyDown(KeyCode.LeftControl))
 {
 }
 
               }
 
               function ChangeAnimation(arrayIndex : int, frames : int, fps : int)
 
                {
     uvAnimationTileX = frames;
     // Calculate index
     framesPerSecond = fps;
     var index : int = Time.time * framesPerSecond;
     // repeat when exhausting all frames
         index = index % (uvAnimationTileX * uvAnimationTileY);
     // Size of every tile
     var size = Vector2 (1.0 / uvAnimationTileX, 1.0 / uvAnimationTileY);
     // split into horizontal and vertical index
      var uIndex = index % uvAnimationTileX;
     var vIndex = index / uvAnimationTileX;
     // build offset
     // y coordinate is the bottom of the image in opengl so we need to invert.
     var offset = Vector2 (uIndex * size.x, 1.0 - size.y - vIndex * size.y);
     renderer.material.SetTexture("_MainTex", textures[arrayIndex]);
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale ("_MainTex", size);
 }
 
               function Jump()
 
               { 
  
               } 
But what would go into that Jump() function?
Thanks in advance.
Neither, as the player only moves along the x axis. Would I need one to use the y axis properly? Or could I get away with using another transform.Translate() in some way?
Your answer