Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to convert this Code into mobile input

Discussion in 'Scripting' started by sidzzz, Sep 22, 2019.

  1. sidzzz

    sidzzz

    Joined:
    Jan 28, 2019
    Posts:
    1
    using UnityEngine;

    public class PlayerController : MonoBehaviour {

    public float moveSpeed = 10f;
    public float rotationSpeed = 10f;

    private float rotation;
    private Rigidbody rb;

    void Start ()
    {
    rb = GetComponent<Rigidbody>();
    }

    void Update ()
    {
    rotation = Input.GetAxisRaw("Horizontal");
    }

    void FixedUpdate ()
    {
    rb.MovePosition(rb.position + transform.forward * moveSpeed * Time.fixedDeltaTime);
    Vector3 yRotation = Vector3.up * rotation * rotationSpeed * Time.fixedDeltaTime;
    Quaternion deltaRotation = Quaternion.Euler(yRotation);
    Quaternion targetRotation = rb.rotation * deltaRotation;
    rb.MoveRotation(Quaternion.Slerp(rb.rotation, targetRotation, 50f * Time.deltaTime));

    }

    }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Hi and welcome, please use code tags. It's the <> button when you are in the text editor here. It adds syntax highlighting and makes everything easier to read.

    You can use Input.GetTouch() to get all the currently active touches on the screen.
    https://docs.unity3d.com/ScriptReference/Input.GetTouch.html

    Depending on the gesture, you can then use these to get, for example, horizontal motion. Each Touch has information about different things, which you can each up on here: https://docs.unity3d.com/ScriptReference/Touch.html

    If you are doing more complex gestures and stuff then you may wanna consider using some asset for it.