Search Unity

FPS Touch camera

Discussion in 'Scripting' started by Flipp3rixPablo, Jul 25, 2019.

  1. Flipp3rixPablo

    Flipp3rixPablo

    Joined:
    Jul 20, 2019
    Posts:
    4
    Hello guys, i got a problem here. Basically i'm developing an FPS smartphone game. The problem comes with camera controls: what i wanted to do was that the camera rotation was made by only dragging the finger on HALF the screen of the smartphone(the left one to be precise). What i made was using the mouse's axes to look around. The issue here is that when i try it on my phone, everytime i tap on the screen the camera would look exactly where i tapped. What i wanted was to smoothly look around without any weird input: take for example any fps game in the industry.( when i tap on the phone,nothing should happen and only when i'm moving my finger on the left side of the screen the camera should follow it)

    Here's the code of what i've done till now

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Mov : MonoBehaviour
    6. {
    7.     public Joystick joystickCamera;
    8.     public GameObject player;
    9.     public float speed = 12.0f, moveSpeed;
    10.     public bool pressed = false;
    11.     Transform cameraTransform;
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         cameraTransform = Camera.main.transform;
    16.         joystickCamera = Joystick.FindObjectOfType<Joystick>();
    17.         player = GameObject.FindGameObjectWithTag("Player");
    18.         Screen.lockCursor = true;
    19.         //cameraTransform.eulerAngles = new Vector3(Mathf.Clamp(cameraTransform.eulerAngles.x, -90, 90), 0, 0);
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         player.transform.Rotate(0, Input.GetAxis("Mouse X") * speed * Time.deltaTime,0);
    26.         this.transform.Rotate(-Input.GetAxis("Mouse Y") * speed * Time.deltaTime,0,0);
    27.  
    28.         if (pressed)
    29.         {
    30.             player.transform.Translate(Vector3.forward * moveSpeed);
    31.         }
    32.     }
    33.  
    34.     public void onPointerDownRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
    35.     {
    36.         pressed = true;
    37.     }
    38.     public void onPointerUpRaceButton(UnityEngine.EventSystems.BaseEventData eventData)
    39.     {
    40.         pressed = false;
    41.     }
    42. }
    43.  
    Any help will be really appreciated.
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,976
    Touch is handled very differently to the mouse, I believe on mobile the MouseX and MouseY will simply either evaluate to X and Y pos on screen of touch, or just be completely incorrect values altogether. Either way, the stuff in this thread:

    https://forum.unity.com/threads/simple-swipe-and-tap-mobile-input.376160/

    will teach you how to do relatively decent reading of touch input such as swiping, dragging etc. You can then call your rotate code from there instead of doing it via a mouse input handler like your doing right now.

    The basics of it:

    Basically your going to read the touch input, get its point, check what its state is (started, dragged, ended, held etc etc) and then check its delta (how much it has moved since it started) to determine which direction the input is in, and then from there you can call your rotate code using the delta to specify which direction the rotate should be in.
     
  3. Flipp3rixPablo

    Flipp3rixPablo

    Joined:
    Jul 20, 2019
    Posts:
    4
    thank a lot :)