Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple 2D Jump Script

Discussion in 'Scripting' started by BobRos, Oct 3, 2015.

  1. BobRos

    BobRos

    Joined:
    Oct 1, 2015
    Posts:
    48
    I'm in the process of making a 2D game, not really a platformer but it'll control like one. So, I have a script that lets my character move left and right, and sprint by pressing the shift key. (See it below)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class PlayerMovement : MonoBehaviour
    4. {
    5.  
    6.     public float speed = 200f;//walk speed
    7.     public float sprint = 300f;//sprint speed
    8.    
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.  
    17.         //sprint script
    18.         if ((Input.GetKey(KeyCode.LeftShift))&(Input.GetKey(KeyCode.D)))//right
    19.         {
    20.             transform.position += new Vector3(sprint * Time.deltaTime, 0.0f, 0.0f);
    21.             transform.localScale = new Vector3(-2, 2, 2);
    22.         }
    23.  
    24.         if ((Input.GetKey(KeyCode.LeftShift)) & (Input.GetKey(KeyCode.A)))//left
    25.         {
    26.             transform.position -= new Vector3(sprint * Time.deltaTime, 0.0f, 0.0f);
    27.             transform.localScale = new Vector3(2, 2, 2);
    28.         }
    29.  
    30.  
    31.         //walk script
    32.         if (Input.GetKey(KeyCode.D))//right
    33.         {
    34.             transform.position += new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
    35.             transform.localScale = new Vector3(-2, 2, 2);
    36.         }
    37.  
    38.         if (Input.GetKey(KeyCode.A))//left
    39.         {
    40.             transform.position -= new Vector3(speed * Time.deltaTime, 0.0f, 0.0f);
    41.             transform.localScale = new Vector3(2, 2, 2);
    42.         }
    43.  
    44.     }
    45. }
    46.  

    So now all I need is a script that lets him jump up and down. He has a rigidbody and a box collider, if that matters. I'm not worried about animations yet, so just a simple jump script. But if possible, I'd want the script to work so that when he's sprinting, he would jump farther than when he's walking. I don't know how much work that would take, so if I'd need to rewrite all my code to do it then don't worry about it. But if possible, he jumps farther when he's sprinting (holding down shift). If you could put it into the code above, I'd really appreciate it, but just the script by itself is fine.
     
  2. GoofBall101

    GoofBall101

    Joined:
    Jul 25, 2015
    Posts:
    57
    Im not much of a coding expert. Defently questioning the Vector3 where your suppose to use Vector2 for 2D. I think theirs a gravity thing for 2d that you could apply Gravity ON/OFF. I would do this. Sorry if Its not much help ;)
    Code (csharp):
    1.  
    2. public float timer = .1f;  
    3.  
    4. //Update thing
    5. timer -= .1f * Time.DeltaTime * 1;
    6. gravity = false  
    7. if (timer <= 0) {
    8. // idk what it is but
    9. gravity= true;
    10. timer = .1f;
    11. }
    12. if (timer == .1f) {
    13. gravity = false;
    14. }
    15.  
     
  3. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    Or you can use this GetComponent<RigidBody>().AddFore(transform.up* A Number to control jump height);