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

2D Player Movement Script

Discussion in '2D' started by coolgamerperson, Jan 17, 2019.

Thread Status:
Not open for further replies.
  1. coolgamerperson

    coolgamerperson

    Joined:
    Feb 16, 2016
    Posts:
    2
    I made a player movement script meant for my 2D game is there anything i should change.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour {
    6.  
    7.     public float speed = 100f;
    8.     public float JumpHeight;
    9.     public bool InAir = false;
    10.  
    11.     private Rigidbody2D rb2d;
    12.  
    13.     void Start() {
    14.         rb2d = GetComponent<Rigidbody2D>();
    15.     }
    16.     private void OnCollisionEnter2D(Collision2D collision)
    17.     {
    18.         InAir = false;
    19.         Debug.Log("InAir false");
    20.     }
    21.     private void OnCollisionExit2D(Collision2D collision)
    22.     {
    23.         InAir = true;
    24.         Debug.Log("InAir True");
    25.     }
    26.  
    27.     void FixedUpdate() {
    28.         Vector2 NoMovement = new Vector2(0f, 0f);
    29.  
    30.         float moveHorizontal = Input.GetAxis("Horizontal");
    31.             if (moveHorizontal > 0)
    32.             {
    33.                 {
    34.                     rb2d.velocity = new Vector2(speed, rb2d.velocity.y);
    35.  
    36.                 }
    37.             }
    38.             if (moveHorizontal < 0)
    39.             {
    40.                 rb2d.velocity = new Vector2(-speed, rb2d.velocity.y);
    41.             }
    42.         if (Input.GetKeyDown(KeyCode.W) || (Input.GetKeyDown(KeyCode.UpArrow))) {
    43.             if (InAir == false)
    44.             {
    45.                 rb2d.AddForce(new Vector2(0, JumpHeight), ForceMode2D.Impulse);
    46.             }
    47.         }
    48.     }
    49.  
    50. }
    51.  
     
    mehdiotsmani123 likes this.
  2. strengthmania6

    strengthmania6

    Joined:
    Jan 7, 2019
    Posts:
    1
    One quick tweak keep this method separate as void Movement()
     
    Oceanweeb likes this.
  3. coolgamerperson

    coolgamerperson

    Joined:
    Feb 16, 2016
    Posts:
    2
    thanks will change
     
  4. bryanbaute80

    bryanbaute80

    Joined:
    Nov 14, 2020
    Posts:
    1
    hippity hoppity, your code is now my property
     
  5. hms0589

    hms0589

    Joined:
    Jan 5, 2020
    Posts:
    27
    do not add InAir bool at collision enter, when player touch wall it can jump infinte
     
  6. hms0589

    hms0589

    Joined:
    Jan 5, 2020
    Posts:
    27
    and do not add jump in fixedupdate, fixed update doesnt work very well in sudden action
     
  7. yayanito

    yayanito

    Joined:
    Dec 10, 2020
    Posts:
    1
     
  8. liki_78

    liki_78

    Joined:
    Apr 1, 2021
    Posts:
    1
    doesnt work none of my keys work i just cant move with the arrow keys i cant with the wasd movment i cant i just cant
     
  9. ImABiSon

    ImABiSon

    Joined:
    Jun 10, 2021
    Posts:
    1
    Same
     
  10. SycamoreGames23

    SycamoreGames23

    Joined:
    Dec 5, 2020
    Posts:
    2
    Same friend, same
     
    robloxmastercaden likes this.
  11. iliketurtles56756

    iliketurtles56756

    Joined:
    Jun 21, 2021
    Posts:
    1
    hipity hopity your code is now my property
     
    dougie_dixon likes this.
  12. Eli_Keys

    Eli_Keys

    Joined:
    Dec 11, 2020
    Posts:
    1
    left and right work but there is a delay for jumps did i do something wrong?
     
    the_newblood likes this.
Thread Status:
Not open for further replies.