How can I make a pc unity game work on phone too?

Started by
5 comments, last by andrei6 3 years, 5 months ago

I have a 3d block slide game, where you have to bypass obstacles to get to the finish line.(pretty basic) You press A and it goe's left, you press D and it goes's right. I want to make the game for android phone too so i right some code to implement these: when i press anywhere on the left side on the screen to go left like you would press a button and the same with the right part. (i am not too good at righting code) and it works very strange, not as it should do.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchControls : MonoBehaviour {


public float moveSpeed;
private object mousePos;

// Use this for initialization
void Start () {
    
}

// Update is called once per frame
void Update () {
    
}

 private void FixedUpdate()
{
    TouchMove();
}

void TouchMove()
{

        if(Input.GetMouseButton(0))
    {

        Vector3 mosuePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (mosuePos.x > 1)
        {

            transform.Translate(moveSpeed, 0, 0);

        }
        else if (mosuePos.x < -1)
        {
            transform.Translate(-moveSpeed, 0, 0);
        }
        }

}
  }
Advertisement
Camera.main.ScreenToWorldPoint

Won't give unified coordinates rather than relative ones depending on where your camera is. So if you move the camera, you won't be able to get reliable results from your checks. Also

Input.GetMouseButton(0)

won't work either on mobile, you have to get results from the touch API

Input.GetTouch(0)

If you want to control your character by simply touching left or right, you don't need to turn the mouse point into screen coords and instead just check if it is in the left or right half of the screen - or - you can detect swipes using the touch API to the left or right side by saving the position of the touch down event and simply check for certain threshold to the left/right side to perform a swipe event

Shaarigan said:
If you want to control your character by simply touching left or right, you don't need to turn the mouse point into screen coords and instead just check if it is in the left or right half of the screen - or - you can detect swipes using the touch API to the left or right side by saving the position of the touch down event and simply check for certain threshold to the left/right side to perform a swipe event

Yes that is want I want to do, but how to control your character by simply touching left or right ?

Shaarigan said:
you don't need to turn the mouse point into screen coords and instead just check if it is in the left or right half of the screen

Shaarigan said:
is in the left or right half of the screen

Ok I understand, but really how do I do that, I am not so good with coding

@undefined can you give me the full code pls

This topic is closed to new replies.

Advertisement