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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Making a FPV script for mobile

Discussion in 'Scripting' started by imimb, Dec 22, 2019.

  1. imimb

    imimb

    Joined:
    Apr 5, 2018
    Posts:
    2
    Hi!
    I've watched this tutorial on Youtube,
    and I want to make a script that takes mouse inputs and rotates the camera and player according to the mouse's position. My problem is that when I run the script from the tutorial in a mobile game I can't drag to change the rotation, like the upper left corner always give the same rotation for the camera and player. I want it so I can drag to change the position wherever on the mobile screen I am. Like in Minecraft PE
    Here's my code so far:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    4.         mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    5.  
    6.         xRotation -= mouseY;
    7.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    8.  
    9.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    10.         playerBody.Rotate(Vector3.up*mouseX);
    11.     }
    Sorry for the bad explination.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    You need to track your own notion of "heading," probably in a float variable.

    Then when the finger touches sweep left/right, you would scale them appropriately for your screen pixel width and your camera view frustum, and (here is the key part), you would add that sweep offset to the heading, then use the heading to turn the camera.

    Screen width in pixels is given by
    Screen.width


    Camera frustum height in degrees is going to be your
    camera.fieldOfView * 2
    , and you would need to multiply it by the
    Screen.width
    , then divide it by the
    Screen.height
    in order to calculate frustum width in degrees.

    When the finger goes down, mark the position so that the heading doesn't jerk, then on subsequent frames that the finger is down, subtract the new position from the previous one.
     
  3. imimb

    imimb

    Joined:
    Apr 5, 2018
    Posts:
    2
    Thanks! I'll try it and reply if I need any more help
     
    Kurt-Dekker likes this.