ThirdPersonTouchMovement

 avatar
unknown
csharp
23 days ago
4.4 kB
7
Indexable
using UnityEngine;

public class ThirdPersonController : MonoBehaviour
{
    [Header("MOVEMENT VARIABLES")]
    private CharacterController characterController;
    [SerializeField] private float runningSpeed = 10f;
    [SerializeField] private Vector2 touchScreenPosition;
    [SerializeField] private Vector2 movementTouchInputValue;
    [SerializeField] private Vector2 currentMovementDirection;

    [Header("CAMERA CONTROLLER")]
    [SerializeField] private Transform mainCamera;
    [SerializeField] private float cameraSensitivity;
    [SerializeField] private Vector2 cameraTouchInputValue;
    [SerializeField] private float xRotation;

    [Header("TOUCH DETECTION")]
    private int leftSideTouch, rightSideTouch; // For Checking/Setting The Touch ID.
    private float halfScreenSize; // For Checking Left And Right Side Of The Phone.
    private float touchInputDeadZone = 10f;

    private void Awake()
    {
        characterController = GetComponent<CharacterController>();
        touchInputDeadZone = Mathf.Pow(Screen.height / touchInputDeadZone, 2);

        leftSideTouch = -1;
        rightSideTouch = -1;
    }

    private void Start()
    {
        halfScreenSize = Screen.width / 2;
    }

    private void Update()
    {
        GetTouchInput(); // Getting Player Touch Input Once A Player Touch's The Screen.

        if (leftSideTouch != -1)
        {
            MovePlayer(); // Moving Player Accordingly To The Touch Input.
        }

        if (rightSideTouch != -1)
        {
            LookAround(); // Looking Around With The Camera Accordingly To The Touch Input.
        }
    }

    private void GetTouchInput()
    {
        for (int i = 0; i < Input.touchCount; i++)
        {
            Touch _touch = Input.GetTouch(i);

            switch (_touch.phase)
            {
                case TouchPhase.Began:

                    if (_touch.position.x < halfScreenSize && leftSideTouch == -1)
                    {
                        leftSideTouch = _touch.fingerId;
                        touchScreenPosition = _touch.position;
                    }
                    else if (_touch.position.x > halfScreenSize && rightSideTouch == -1)
                    {
                        rightSideTouch = _touch.fingerId;
                    }

                    break;
                case TouchPhase.Moved:

                    if (leftSideTouch != -1)
                    {
                        movementTouchInputValue = _touch.position - touchScreenPosition;
                    }
                    
                    if (rightSideTouch != -1)
                    {
                        cameraTouchInputValue = _touch.deltaPosition * cameraSensitivity * Time.deltaTime;
                    }

                    break;
                case TouchPhase.Stationary:

                    if (_touch.fingerId == rightSideTouch)
                    {
                        cameraTouchInputValue = Vector2.zero;
                    }

                    break;
                case TouchPhase.Ended:
                case TouchPhase.Canceled:

                    if (_touch.fingerId == leftSideTouch)
                    {
                        leftSideTouch = -1;
                    }
                    else if (_touch.fingerId == rightSideTouch)
                    {
                        rightSideTouch = -1;
                    }
                    touchScreenPosition = Vector2.zero;
                    cameraTouchInputValue = Vector2.zero;
                    movementTouchInputValue = Vector2.zero;

                    break;
            }
        }
    }

    private void MovePlayer()
    {
        if (movementTouchInputValue.sqrMagnitude <= touchInputDeadZone) return; // Don't Move If Touch Isn't Being Dragged A Bit Further

        currentMovementDirection = movementTouchInputValue.normalized * runningSpeed * Time.deltaTime;
        characterController.Move(transform.right * currentMovementDirection.x + transform.forward * currentMovementDirection.y);
    }

    private void LookAround()
    {
        xRotation = Mathf.Clamp(xRotation - cameraTouchInputValue.y, -90f, 90f);
        mainCamera.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

        transform.Rotate(Vector3.up, cameraTouchInputValue.x);
    }
}
Leave a Comment