using System.Collections; using System.Collections.Generic; using UnityEngine; public class FirstPersonController : MonoBehaviour { // References public Transform cameraTransform; public CharacterController characterController; // Player settings public float cameraSensitivity; public float moveSpeed; public float moveInputDeadZone; // Touch detection int leftFingerId, rightFingerId; float halfScreenWidth; float mostScreenHeight; // Camera control Vector2 lookInput; float cameraPitch; // Player movement Vector2 moveTouchStartPosition; Vector2 moveInput; bool activeCtrl; // Start is called before the first frame update void Start() { // id = -1 means the finger is not being tracked leftFingerId = -1; rightFingerId = -1; // only calculate once halfScreenWidth = Screen.width / 2; mostScreenHeight = Screen.height * 7 / 10; activeCtrl = true; // calculate the movement input dead zone moveInputDeadZone = Mathf.Pow(Screen.height / moveInputDeadZone, 2); } public void activateControl() { activeCtrl = true; } public void deactivateControl() { activeCtrl = false; } // Update is called once per frame void Update() { if(activeCtrl == true) { GetTouchInput(); if (rightFingerId != -1) { // Ony look around if the right finger is being tracked Debug.Log("Rotating"); LookAround(); } if (leftFingerId != -1) { // Ony move if the left finger is being tracked Debug.Log("Moving"); Move(); } } } // Handles input void GetTouchInput() { // Iterate through all the detected touches for (int i = 0; i < Input.touchCount; i++) { Touch t = Input.GetTouch(i); // Check each touch's phase switch (t.phase) { case TouchPhase.Began: if (t.position.x < halfScreenWidth && leftFingerId == -1 && t.position.y < mostScreenHeight) { // Start tracking the left finger if it was not previously being tracked leftFingerId = t.fingerId; // Set the start position for the movement control finger moveTouchStartPosition = t.position; } else if (t.position.x > halfScreenWidth && rightFingerId == -1 && t.position.y < mostScreenHeight) { // Start tracking the rightfinger if it was not previously being tracked rightFingerId = t.fingerId; } break; case TouchPhase.Ended: case TouchPhase.Canceled: if (t.fingerId == leftFingerId) { // Stop tracking the left finger leftFingerId = -1; Debug.Log("Stopped tracking left finger"); } else if (t.fingerId == rightFingerId) { // Stop tracking the right finger rightFingerId = -1; Debug.Log("Stopped tracking right finger"); } break; case TouchPhase.Moved: // Get input for looking around if (t.fingerId == rightFingerId) { lookInput = -t.deltaPosition * cameraSensitivity * Time.deltaTime; } else if (t.fingerId == leftFingerId) { // calculating the position delta from the start position moveInput = t.position - moveTouchStartPosition; } break; case TouchPhase.Stationary: // Set the look input to zero if the finger is still if (t.fingerId == rightFingerId) { lookInput = Vector2.zero; } break; } } } void LookAround() { // vertical (pitch) rotation cameraPitch = Mathf.Clamp(cameraPitch - lookInput.y, -90f, 90f); cameraTransform.localRotation = Quaternion.Euler(cameraPitch, 0, 0); // horizontal (yaw) rotation transform.Rotate(transform.up, lookInput.x); } void Move() { // Don't move if the touch delta is shorter than the designated dead zone if (moveInput.sqrMagnitude <= moveInputDeadZone) return; // Multiply the normalized direction by the speed Vector2 movementDirection = moveInput.normalized * moveSpeed * Time.deltaTime; // Move relatively to the local transform's direction characterController.Move(transform.right * movementDirection.x + transform.forward * movementDirection.y); } }