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

my player does no stand up while moving

Discussion in 'Getting Started' started by GodOfTheFlies, Jun 18, 2020.

  1. GodOfTheFlies

    GodOfTheFlies

    Joined:
    Jun 18, 2020
    Posts:
    1
    hi im new at coding and i wrote this simple code for movement but after i move a bit the player trips and falls...
    it still moves but it looks weird. Do anybody knows how to fix it? here is my code:



    private Rigidbody2D rb;
    public Joystick joystick;
    private bool facingright = true;
    public LayerMask whatisground;
    private bool canjump;



    void Start() {
    rb = GetComponent<Rigidbody2D>();

    }

    private void FixedUpdate()
    {
    moveInput = joystick.Horizontal * speed;
    Debug.Log(moveInput);
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);


    }


    public void jump(){
    if (canjump) { rb.velocity = new Vector2(rb.velocity.x, jumpforce); }

    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "ground") ;
    canjump = true;
    }
    private void OnCollisionExit2D(Collision2D collision)
    {
    if (collision.gameObject.tag == "ground") ;
    canjump = false;
    }
    }
     

    Attached Files: