Search Unity

FREE Simple 2D Platformer Script!

Discussion in 'Assets and Asset Store' started by sothern101, Mar 14, 2014.

  1. sothern101

    sothern101

    Joined:
    Jan 30, 2014
    Posts:
    28
    Hello everyone! This here is a simple platformer script for a 2D game I made. Thought i'd share it around since some people are having trouble making some!
    Attach this to any object (preferably 2d) as well with a 2d box collider and a rigidbody 2d.

    Code (csharp):
    1.  
    2.  
    3. var movementSpeed : float = 3f;
    4. var jumpSpeed : float = 500f;
    5. var isGrounded : boolean = true;
    6.  
    7. function Update ()
    8. {
    9.     if(isGrounded){
    10.         rigidbody2D.gravityScale = 2.1;
    11.     }
    12.    
    13.     if(Input.GetKey(KeyCode.LeftArrow)){
    14.         transform.position += Vector3.left * movementSpeed * Time.deltaTime;
    15.         transform.rotation.y = 180; //This is for rotating sprite images
    16.     }
    17.     if(Input.GetKey(KeyCode.RightArrow)){
    18.         transform.position += Vector3.right * movementSpeed * Time.deltaTime;
    19.         transform.rotation.y = 0; //This is for rotating sprite images
    20.     }
    21.     if(Input.GetKey(KeyCode.DownArrow)){
    22.         if(!isGrounded){
    23.             rigidbody2D.gravityScale += 2;
    24.         }
    25.     }
    26.     if(Input.GetKeyDown(KeyCode.UpArrow)){
    27.         if(isGrounded){
    28.             rigidbody2D.AddForce(Vector3.up * jumpSpeed);
    29.             isGrounded = false;
    30.         }
    31.     }
    32. }
    33.  
    34. function OnCollisionEnter2D (col : Collision2D)
    35. {
    36.     isGrounded = true;
    37. }
     
    Haniwa55 and mawoopy like this.
  2. DerpCreat0ns

    DerpCreat0ns

    Joined:
    Jun 2, 2020
    Posts:
    1
  3. clickinggames

    clickinggames

    Joined:
    Jun 6, 2020
    Posts:
    2
    this code is so old that every bit of your work is a bug
     
    krispastas and leopil like this.
  4. jlp-141304

    jlp-141304

    Joined:
    Aug 24, 2021
    Posts:
    1
    I don't know how to use Unity
     
  5. Fusquinha23

    Fusquinha23

    Joined:
    Oct 5, 2021
    Posts:
    1
    Bananão
    o_O
     
  6. unity_125236

    unity_125236

    Joined:
    Dec 1, 2022
    Posts:
    1
    l
     
  7. Deleted User

    Deleted User

    Guest

    13 Errors?! I don't think it works now :(
     
  8. Rim32

    Rim32

    Joined:
    Sep 16, 2022
    Posts:
    3
    Code (CSharp):
    1. public float movementSpeed = 3f;
    2.     public float jumpSpeed = 500f;
    3.     private bool isGrounded = true;
    4.     private Rigidbody2D rb;
    5.  
    6.     private void Start()
    7.     {
    8.         rb = GetComponent<Rigidbody2D>();
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         if (isGrounded)
    14.         {
    15.             rb.gravityScale = 2.1f;
    16.         }
    17.  
    18.         if (Input.GetKey("a"))
    19.         {
    20.             transform.position += Vector3.left * movementSpeed * Time.deltaTime;
    21.            
    22.         }
    23.         if (Input.GetKey("d"))
    24.         {
    25.             transform.position += Vector3.right * movementSpeed * Time.deltaTime;
    26.         }
    27.         if (Input.GetKey("s"))
    28.         {
    29.             if (!isGrounded)
    30.             {
    31.                 rb.gravityScale += 2;
    32.             }
    33.         }
    34.         if (Input.GetKeyDown(KeyCode.Space))
    35.         {
    36.             if (isGrounded)
    37.             {
    38.                 rb.AddForce(Vector3.up * jumpSpeed);
    39.                 isGrounded = false;
    40.             }
    41.         }
    42.     }
    43.  
    44.     private void OnCollisionEnter2D(Collision2D collision)
    45.     {
    46.         isGrounded = true;
    47.     }
     
    LarissPng likes this.
  9. CDDGamerBoy

    CDDGamerBoy

    Joined:
    Apr 30, 2023
    Posts:
    1
    Hippity hoppity, your code is my property
     
    margaritapeter13 likes this.
  10. KJPlayer

    KJPlayer

    Joined:
    Sep 6, 2023
    Posts:
    1
    I prefer to use bippity boppity, but that works too.