- Home /
Touch Controls for mobile
Hello. I tried in many ways to make touch control for android. But nothing worked. Control of the keyboard works perfectly ... can someone help me? A good tutorial? Or any advice for this script?
This 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;
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;
}
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;
}
}
Comment
Answer by Graham-Dunnett · Feb 22, 2015 at 04:33 PM
The examples on the Input documentation are pretty useful.