Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Mobile Movement Question

Discussion in 'Editor & General Support' started by BenBonkGames, Jul 15, 2018.

  1. BenBonkGames

    BenBonkGames

    Joined:
    Jul 13, 2018
    Posts:
    16
    Hi so i'm making a 2d mobile game and I have the basic player controls done for left and right so whenever you press A on your keyboard you go left and D is right. But I wanted to make it instead whenever you touch right on your phone screen you move right I've tried some of Unity's Standard Assets, but I cant really figure out some of them. Thanks!

    PS I attached my movement script below.
     

    Attached Files:

  2. Moonjump

    Moonjump

    Joined:
    Apr 15, 2010
    Posts:
    2,571
    Firstly, I suggest changing your post so that the code is in your post, not an image. You will get more replies that way. There is an Insert Code option at the top of the post editing window.

    As for your issue, look at Unity's Input.GetTouch for example code that is slightly different, but you should be able to work out your needs from it.

    One other thing you might have to resolve after that: deltaTime is usually used in Update to compensate for the variable time between frames, but not FixedUpdate which has a fixed time. You have it the other way, although you have used fixedDeltaTime, but deltaTime on the end of the moveVelocity line instead would probably work better.
     
  3. BenBonkGames

    BenBonkGames

    Joined:
    Jul 13, 2018
    Posts:
    16
    Hi thanks for your reply, but I'm A bit confused. (Sorry I'm fairly new to Unity). Whenever I invest Input.GetTouch it messes up the strings Horizontal and Vertical and says they have to be integers or something any help?
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.  
    9.     public float speed;
    10.     private Rigidbody2D rb;
    11.     private Vector2 moveVelocity;
    12.  
    13.    void Start()
    14.     {
    15.         rb = GetComponent<Rigidbody2D>();
    16.  
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         Vector2 moveInput = new Vector2(Input.GetTouch("Horizontal"), Input.GetTouch("Vertical"));
    22.         moveVelocity = moveInput.normalized * speed;
    23.     }
    24.  
    25.     void FixedUpdate()
    26.     {
    27.         rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    28.     }
    29. }
    30.  
    31.  
    32.  
    33.  
    34.  
    35.