Hello,
I am having problems when configuring my character movement. The thing is, I am trying to do it with on screen buttons for mobile devices. When I use the button tu jump it works good, but the buttons for horizontal movement make my character move non stop. I am very new with Unity so I really have no clue what can be the problem. I will appreciate any help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMoveJoystick : MonoBehaviour
{
Rigidbody2D rb2D;
public float runSpeed = 1.25f;
public float jumpSpeed = 3;
public float runSpeedHorizontal = 2;
public SpriteRenderer spriteRenderer;
public Animator animator;
private void Start()
{
rb2D = GetComponent<Rigidbody2D>();
}
private void Update()
{
if (rb2D.velocity.x > 0)
{
spriteRenderer.flipX = false;
animator.SetBool("Run", true);
}
else if (rb2D.velocity.x < 0)
{
spriteRenderer.flipX = true;
animator.SetBool("Run", true);
}
else
{
animator.SetBool("Run", false);
}
if (Input.GetKey("space") && CheckGround.isGrounded)
{
rb2D.velocity = new Vector2(rb2D.velocity.x, jumpSpeed);
}
if (CheckGround.isGrounded == false)
{
animator.SetBool("Jump", true);
animator.SetBool("Run", false);
}
if (CheckGround.isGrounded == true)
{
animator.SetBool("Jump", false);
animator.SetBool("Falling", false);
}
if (rb2D.velocity.y < 0)
{
animator.SetBool("Falling", true);
}
else if (rb2D.velocity.y > 0)
{
animator.SetBool("Falling", false);
}
}
public void buttonJump()
{
if (CheckGround.isGrounded)
{
rb2D.velocity = new Vector2 (rb2D.velocity.x, jumpSpeed);
}
}
public void buttonRight()
{
rb2D.velocity = new Vector2 (runSpeed, rb2D.velocity.y);
spriteRenderer.flipX = false;
animator.SetBool("Run", true);
}
public void buttonLeft()
{
rb2D.velocity = new Vector2 (-runSpeed, rb2D.velocity.y);
spriteRenderer.flipX = true;
animator.SetBool("Run", true);
}
}