Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Buttons to make 2d character to move left and right

Discussion in 'Scripting' started by hamzah2412, May 9, 2020.

  1. hamzah2412

    hamzah2412

    Joined:
    Jul 8, 2019
    Posts:
    9
    Does anyone know how to script so that 2d character can move left and right using left or right button? I have made the movements to work but the buttons doesn't seem to work. help asap
     
  2. hamzah2412

    hamzah2412

    Joined:
    Jul 8, 2019
    Posts:
    9
    Code (CSharp):
    1. float h = Input.GetAxisRaw("Horizontal"); //Movement Variable
    2.          
    3.             //Movement Controls
    4.             rb.velocity = new Vector2(moveSpeed * h, rb.velocity.y);        
    5.             if (h > 0) //when its more than 0 it will face right
    6.             {          
    7.                 transform.localScale = new Vector3(1, 1, 1);            
    8.             }
    9.             else if (h < 0) //when its less than 0 it will face left
    10.             {
    11.                 transform.localScale = new Vector3(-1, 1, 1);            
    12.             }
    13.             rb.velocity = new Vector2(moveSpeed * h, rb.velocity.y);
    this script helps control my movement of the 2d character.
     
    Last edited: May 10, 2020