Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Change How to convert from WASD to Touch controls

Hi,

I am extremely new to unity and C# coding. I have completed a simple Cube game by following Brackeys how to make a game tutorial

I have it working on all aspects except when i play it on my Android i cannot control as i have it set for keyboard WASD controls. I cant seem to understand how to make this work on touch screens.


tried using a few different methods but i get errors. Below is my script for movement that currently has no errors and works fine on PC's keyboards just not on Android touch screens(if possible without a "joystick" or "button UI" if at all possible;


I appreciate any help and thank you in advance.

using UnityEngine;


public class PlayerMovement : MonoBehaviour{


// This is used to reference the Rigidbody componant called "rb"

public Rigidbody rb;


public float forwardForce = 2000f;

public float sidewaysForce = 500f;


// Update is called once per frame

// Always use "FixedUpdate" when "messing" with physics

void FixedUpdate()

{

// Add a forward force

rb.AddForce(0, 0, forwardForce * Time.deltaTime);


if (Input.GetKey("d"))

{

rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);

}

if (Input.GetKey("a"))

{

rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);


}


if (rb.position.y < -1f)

{

FindObjectOfType<GameManager>().EndGame();

}

}

}

Please Help

i should note I m not asking to have someone write the code for me but rather explain how to implement a touch input instead or as well as WASD(keyboard) or reference a page or thread to point me in the proper direction.

Thanks again 

Answers

  • Roby65Roby65 Member

    You can follow this tutorial: https://www.youtube.com/watch?v=bp2PiFC9sSs

    About your current code, I would like to suggest using GetAxis, like in this tutorial: https://www.youtube.com/watch?v=_QajrabyTJc

  • danieldaniel Member

    but how du you want to move then if not with joystick or button UI? :)

  • 0bee10bee1 Member

    I want to move them with just touch and no UI Elements..

    see the code down I tried something but it seems very jittery and it seems that it is lagging i dont know why

    -----VVVVVV

    void FixedUpdate()

      { // for Mobile Controlls

        if (Input.touchCount > 0)

        {

          // for forward movement

          if (canmove == true)

          { rb.AddForce(0, 0, ForwardForce * Time.deltaTime); }


          touch = Input.GetTouch(0);

          if (touch.phase == TouchPhase.Moved)

          {

            transform.position = new Vector3(

              transform.position.x + touch.deltaPosition.x * speedModifier,

              transform.position.y, transform.position.z);

             

            rotateY = Quaternion.Euler(

              0f, 0f, -touch.deltaPosition.x * rotatespeedModifier);

            transform.rotation = rotateY * transform.rotation;

          }

        }

        else if(Input.touchCount < 0)

        {

     canmove = false;  

        }

  • if you uses a joystick just do that

    public Joystick joystick;
    
    if (joystick.Vertical > 0.1f) {
       rb.AddForce(Vector3.forward * Time.deltaTime * force);
    }
    if (joystick.Vertical < -0.1f) {
       rb.AddForce(Vector3.forward * Time.deltaTime * -force);
    }
    if (joystick.Horizontal > 0.1f) {
       rb.AddForce(Vector3.right* Time.deltaTime * force);
    }
    if (joystick.Horizontal < -0.1f) {
       rb.AddForce(Vector3.right * Time.deltaTime * -force);
    }
    

    you can also use somekind of map function that remaps the joysticks verticle and horizontal values to convert the force from 0 to max stat and it will be more efficient.

  • 0bee10bee1 Member

    no i do not want to use Ui or joystick

  • Well, you could use touch.position to move the player. Keep the script as short and simple as possible. Else, you will face a big problem when you want to check for errors.

Sign In or Register to comment.