Answer the question
In order to leave comments, you need to log in
Why doesn't half of the clicks work?
Like half of the clicks do not work, or will I release my finger, and he continues to think that he is on the screen?
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Submarine : MonoBehaviour
{
[Header("Settings")]
[SerializeField] private float _speed;
[SerializeField] private float _rotationSpeed;
[Header("Components")]
[SerializeField] private Rigidbody _rigitbody;
private int _leftTouchId;
private int _rightTouchId;
private int _halfScreen;
private void Awake()
{
_halfScreen = Screen.width / 2;
_leftTouchId = -1;
_rightTouchId = -1;
}
private void FixedUpdate()
{
GetTouchInput();
if(_leftTouchId != -1)
{
RotateSubmarine();
}
if(_rightTouchId != -1)
{
MoveSubmarine();
}
}
private void RotateSubmarine()
{
transform.Rotate(Vector3.forward, _rotationSpeed * Time.fixedDeltaTime);
}
private void MoveSubmarine()
{
_rigitbody.velocity = transform.right * _speed * Time.fixedDeltaTime;
}
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 < _halfScreen && _leftTouchId == -1)
{
// Start tracking the left finger if it was not previously being tracked
_leftTouchId = touch.fingerId;
Debug.Log("Start left");
}
else if (touch.position.x > _halfScreen && _rightTouchId == -1)
{
// Start tracking the rightfinger if it was not previously being tracked
_rightTouchId = touch.fingerId;
Debug.Log("Start right");
}
break;
case TouchPhase.Ended:
case TouchPhase.Canceled:
if (touch.fingerId == _leftTouchId)
{
// Stop tracking the left finger
_leftTouchId = -1;
Debug.Log("Stopped tracking left finger");
}
else if (touch.fingerId == _rightTouchId)
{
// Stop tracking the right finger
_rightTouchId = -1;
Debug.Log("Stopped tracking right finger");
}
break;
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question