Hello, I am trying to implement horizontal movement of my main character in the game. I use the Axis Touch Button script to move the player and this script for the Player:
using System.Collections;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class Player : MonoBehaviour {
float directionX;
public float speed = 15f;
public float mapWidth = 5f;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate ()
{
directionX = CrossPlatformInputManager.GetAxis("Horizontal") * Time.fixedDeltaTime * speed;
Vector2 newPosition = rb.position + Vector2.right * directionX;
newPosition.x = Mathf.Clamp(newPosition.x, -mapWidth, mapWidth);
rb.MovePosition(newPosition);
The problem is when I move the player left or right (by pressing left or right button) it keeps moving slowly to the same direction (after I have finished pressing that button) and it does not stop until I press the button for opposite direction.