- Home /
Touch input
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;
}
}
Add some Debug to find out if the buttons are actually being pressed.
yes ... They are pressed because my player is facing right and left ... but he's not moving ...
Answer by kbaloch · Feb 19, 2015 at 12:45 PM
you can not use like this, you should use OnGui() for input like this
void OnGUI() {
if(GUI.Button(new Rect("your button positon"),"Right")) {
// your staf code goes here for right button
} else
if(GUI.Button(new Rect("your button positon"),"Left")) {
// your staf code goes here for left button
}
}
With OnGUI don't work ... It make my player moving and I can't stop it ...
use bool values. set it true in gui for left and right and change its value to false after your move code with the use of conditions
Now it works ... but don't work to press multiple buttons ... Ex:press right and jump