- Home /
Input stays the same no matter the rotation ?
Hi,
I have a question about my script and how could I improve it to fix my issue
So when my player is going straight everything’s fine and I can swipe left/right but when the player is rotating by 90* (because it's a child to the camera) the left/right controls are turning into forward/backwards. How can I "lock" my swipes direction to stay exactly the same no matter in which direction the camera will go ?
public class NewSwipeScript : MonoBehaviour {
private bool routineRunning;
[SerializeField] private float swipeDistance = 5;
// configure this in the Inspector
// How long should the swiping take in seconds
[SerializeField] private float swipeDuration = 1;
private Vector3 _startpos; // start position
private Vector3 _endpos; //end position
public int pozioma = 0;
public int pionowa = 0;
public Animator _anim;
private void Start()
{
GetComponent<Animator>();
}
private void Update()
{
if (routineRunning) return;
foreach (var touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began:
_startpos = touch.position;
_endpos = touch.position;
break;
case TouchPhase.Moved:
_endpos = touch.position;
break;
case TouchPhase.Ended:
if (Mathf.Abs(_startpos.y - _endpos.y) > Mathf.Abs(_startpos.x - _endpos.x))
{
if (_startpos.y - _endpos.y > 100 && pionowa > -1) //swipe down
{
pionowa--;
StartCoroutine(MoveSmooth(transform.up * -1, swipeDuration));
}
else if (_startpos.y - _endpos.y < -100 && pionowa < 1) //swipe up
{
pionowa++;
StartCoroutine(MoveSmooth(transform.up, swipeDuration));
}
}
else
{
if (_startpos.x - _endpos.x > 100 && pozioma > -1) //swipe left
{
pozioma--;
StartCoroutine(MoveSmooth(transform.right * -1, swipeDuration));
_anim.SetTrigger("FlyLeft");
}
else if (_startpos.x - _endpos.x < -100 && pozioma < 1) //swipe right
{
pozioma++;
StartCoroutine(MoveSmooth(transform.right, swipeDuration));
_anim.SetTrigger("FlyRight");
}
}
break;
}
}
}
private IEnumerator MoveSmooth(Vector3 direction, float duration)
{
routineRunning = true;
var currentPosition = transform.position;
var targetPosition = currentPosition + direction * swipeDistance;
var timePassed = 0f;
do
{
var lerpFactor = Mathf.SmoothStep(0, 1, timePassed / duration);
// using the factor between 0 and 1
transform.position = Vector3.Lerp(currentPosition, targetPosition, lerpFactor);
// increase by time since last frame
timePassed += Time.deltaTime;
// let this frame render and go on from
// here in the next frame
yield return null;
} while (timePassed <= duration);
transform.position = targetPosition;
routineRunning = false;
}
}
Is this script attatched to the parent of the gameobject your rotating or a child, if a child is the Camera the parent?
The script is attached to the player - child of my camera and all of this is a chid of an empty game object which I'm rotating
Ok, first off, you would be better with the player gameobject with the script as parent with the camera as child. I'm pretty sure that alone would fix your issue.