- Home /
How to implement mobile controls
Hey Unity Community this is my first question here. Im designing a running game but different from temple run or subway surfer . The player can run in any direction so i have created the game for pc and its works perfect . Now i would like to make it happen on the mobile also. I would like to know how to implement player movement on a mobile device . Given below code is the code for player movement that i've already used on the pc version. It would be helpful if someone could change the code so that it works the same on the mobile.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MovePlayer : MonoBehaviour {
public float speed = 6.0F;
public float jumpSpeed = 8.0F;
public float gravity = 20.0F;
public float rotateSpeed = 3.0F;
private Vector3 moveDirection = Vector3.zero;
Animator anim;
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
var x = Input.GetAxis("Horizontal");
var z = Input.GetAxis("Vertical");
anim.SetFloat("BlendX", x);
anim.SetFloat("BlendY", z);
CharacterController controller = GetComponent<CharacterController>();
if (controller.isGrounded)
{
moveDirection = new Vector3(0, 0, z);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton("Jump"))
moveDirection.y = jumpSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
//Rotate Player
transform.Rotate(0, x * 1.5f, 0);
}
}
Your answer
Follow this Question
Related Questions
Why Do My Obstacles Do This In My Object Pooler? 1 Answer
How to increment value of axis when something happens 2 Answers
I need help with a Text Location script (Ho bisogno di aiuto con uno script di posizione del testo) 0 Answers
How can i place subjects in different positions? 0 Answers
Making a gameobject move to a position 2 Answers