- Home /
Touch Input for character control using mechanim
pls help!! i need help with a code that moves my player using mechanim states on an android device . Now i have the player working well but when it comes to making him move horizontally or vertically with a touch input, it becomes a problem. Pls what is the equivalent of float h = Input.GetAxis("Horizontal"); and float v = Input.GetAxis("Vertical"); for a touch input?
Here is the code i used
using UnityEngine; using System.Collections;
public class IdleJump2 : MonoBehaviour {
//GUITexture guitexture;
GUITexture UpButton;
GUITexture DownButton;
GUITexture LeftButton;
GUITexture RightButton;
GUITexture JumpButton;
GUITexture DiveButton;
GUITexture TurnLeftButton;
GUITexture TurnRightButton;
public Vector3 originalPosition;
public Quaternion originalRotation ;
protected Animator animator;
public float DirectionDampTime = .25f;
public bool ApplyGravity = true;
// Use this for initialization
void Start ()
{
animator = GetComponent<Animator>();
if(animator.layerCount >= 2)
animator.SetLayerWeight(1, 1);
}
// Update is called once per frame
void Update ()
{
foreach ( Touch touch in Input.touches)
{
if (animator)
{
AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
if (stateInfo.IsName("Base Layer.Idle"))
{
if (touch.phase == TouchPhase.Stationary && TurnRightButton.HitTest (touch.position))
{
animator.SetBool("Turn", true);
}
if (touch.phase == TouchPhase.Stationary && TurnRightButton.HitTest (touch.position))
{
animator.SetBool("TurnLeft", true);
}
}
else
{
animator.SetBool("Turn", false);
animator.SetBool("TurnLeft", false);
animator.SetBool("Climb", false);
}
if (stateInfo.IsName("Base Layer.Run"))
{
if (touch.phase == TouchPhase.Stationary && JumpButton.HitTest (touch.position))
{
animator.SetBool("Jump", true);
}
if (touch.phase == TouchPhase.Stationary && DiveButton.HitTest (touch.position))
{
animator.SetBool("Dive", true);
}
else
{
animator.SetBool("Jump", false);
animator.SetBool("Dive", false);
animator.SetBool("Climb", false);
}
}
if(Input.GetButtonDown("Fire2") && animator.layerCount >= 2)
{
animator.SetBool("Hi", !animator.GetBool("Hi"));
}
float h = Input.GetAxis("Horizontal");// This is where i need help .PLS what is the equivalent of this statement for a touch input?
float v = Input.GetAxis("Vertical"); // This is where i need help .PLS what is the equivalent of this statement for a touch input?
animator.SetFloat("Speed", h*h+v);
animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime);
}
}
}
void Awake() {
originalPosition = transform.position;
originalRotation = transform.rotation;
UpButton = GameObject.Find ("arrowUp").guiTexture;
DownButton = GameObject.Find ("arrowDown").guiTexture;
LeftButton = GameObject.Find ("arrowLeft").guiTexture;
RightButton = GameObject.Find ("arrowRight").guiTexture;
JumpButton = GameObject.Find ("arrowRight").guiTexture;
DiveButton= GameObject.Find ("arrowRight").guiTexture;
}
void resetGame() {
// Reset to original position
transform.position = originalPosition;
transform.rotation = originalRotation;
}
}
Your answer
Follow this Question
Related Questions
Double Touch Tap to Select Android 0 Answers
Get current touch position 4 Answers
Free Version of Android Market by developing registration please confirm that you have problems. 0 Answers
Create custom mobile keyboard in Unity? 1 Answer
Casting a ray to detect the touched object in world space does not work 3 Answers