Question by
Christo_deWit · Oct 25, 2018 at 07:56 PM ·
touch controlstouchscreen
Unity Touch Input
Hi everyone
Basically I want my player object to rotate around an axis, I have been able to do this with input.GetHorizontal but now I want to make this work with touch controls. Below is the code but it has not been able to work and I cant figure it out. Please help.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Player : MonoBehaviour {
public float moveSpeed = 600f;
float movement = 0f;
// Update is called once per frame
void Update ()
{
if (Input.touchCount > 0)
{
movement = Input.GetTouch(0);
if(movement.phase == TouchPhase.Began)
{
if (touch.position.x < Screen.width / 2)
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime);
if (touch.position.x > Screen.width / 2)
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
}
}
private void FixedUpdate()
{
transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
}
private void OnTriggerEnter2D(Collider2D collision) {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Comment
Your answer
Follow this Question
Related Questions
New input system and touch on webgl 1 Answer
Unity 2d android game How to "freeze" position on Y axis 1 Answer
Touchscreen Movement?? 1 Answer
Joystick + swipe question? 0 Answers
Rotate object by touch using Angles? 0 Answers