N
N
N0nV3r2022-03-22 21:59:01
Unity
N0nV3r, 2022-03-22 21:59:01

Control not working in Unity2D?

Briefly: the game is a 2D platformer, the control only works, probably in the first second it washes less!
Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
  public float speed;
  public float jumpForce;
  private float moveInput;

  private Rigidbody2D rb;

  private bool facingRight = true;

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


  private void Update()
  {
    moveInput = Input.GetAxis("Horizontal");
    rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
    if(facingRight == false && moveInput > 0)
    {
      Flip();
    }
    else if(facingRight == true && moveInput < 0)
    {
      Flip();
    }
  }

  void Flip()
  {
    facingRight = !facingRight;
    Vector3 Scaler = transform.localScale;
    Scaler.x *= -1;
    transform.localScale = Scaler;
  }
}

Thank you!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question