Hi everyone. I have a Player script to control my player. My keyboard controls are working perfectly. But my problem is at my mobile touch input. The jump input work… But the right and left arrows are don’t. I’m calling the Left and Right functions with the unity UI button.
Here is my script :
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
private bool _isFacingRight;
private CharacterController2D _controller;
private float _normalizedHorizontalSpeed;
public float MaxSpeed = 8;
public float SpeedAccelerationOnGround = 10f;
public float SpeedAccelerationInAir = 5f;
public bool IsDead { get; private set; }
public void Awake(){
_controller = GetComponent<CharacterController2D>();
_isFacingRight = transform.localScale.x > 0;
}
public void Update()
{
if (!IsDead)
HandleInput();
var movementFactor = _controller.State.IsGrounded ? SpeedAccelerationOnGround : SpeedAccelerationInAir;
// Resharper disable ConvertIfStateToConditionalTernaryExpression
if(IsDead)
_controller.SetHorizontalForce(0);
else
_controller.SetHorizontalForce(Mathf.Lerp(_controller.Velocity.x, _normalizedHorizontalSpeed * MaxSpeed, Time.deltaTime * movementFactor));
}
public void Kill()
{
_controller.HandleCollisions = false;
collider2D.enabled = false;
IsDead = true;
_controller.SetForce(new Vector2(0, 20));
}
public void RespawnAt(Transform spawnPoint)
{
if (!_isFacingRight)
Flip();
IsDead = false;
collider2D.enabled = true;
_controller.HandleCollisions = true;
transform.position = spawnPoint.position;
}
public void InputRight(){
{
_normalizedHorizontalSpeed = 1;
if (!_isFacingRight)
Flip();
}
}
public void InputLeft(){
if (_isFacingRight)
Flip();
_normalizedHorizontalSpeed = -1;
}
public void InputJump(){
if (_controller.CanJump)
{
_controller.Jump();
}
}
void HandleInput()
{
if (Input.GetKey(KeyCode.D))
{
_normalizedHorizontalSpeed = 1;
if (!_isFacingRight)
Flip();
}
else if (Input.GetKey(KeyCode.A))
{
_normalizedHorizontalSpeed = -1;
if (_isFacingRight)
Flip();
}
else
{
_normalizedHorizontalSpeed = 0;
}
if (_controller.CanJump && Input.GetKeyDown(KeyCode.Space))
{
_controller.Jump();
}
}
private void Flip()
{
transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
_isFacingRight = transform.localScale.x > 0;
}
}