- Home /
Question by
Bogdan003 · Apr 03, 2020 at 11:48 PM ·
mobiletouchscreenswipe
Swipe and touch alternation
I made a script to move the player to a target when i touch the screen,and another one to rotate the camera when i swipe it.However the player seems to move when i swipe, i tried creating a bool which tells if i'm swiping or not but it didn't work.The result i am trying to achieve is the player not to move when i swipe to change the camera rotation.
Player code:
void Update()
{
if (SwipeInput.swipedUp || Input.GetKeyDown(KeyCode.Space))
{
FindObjectOfType<GameManager>().EndGame();
}
if (Input.touchCount >0 )//&& Time.time >= nextTimeToFire)
{
// GameObject camchild = GameObject.Find("CamChild");
// SwipeInput swipe = camchild.GetComponent<SwipeInput>();
// if (swipe.isSwiping == false)
//{
Ray ray = cam.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Walkable"))
{
agent.SetDestination(hit.point);
}
else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Enemy"))
{
targeting = true;
targetEnemy = hit.transform.gameObject;
targetPoint = hit.point;
targetRotation = Quaternion.LookRotation(hit.point - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotspeed * Time.deltaTime);
Shoot();
}
}
}
// }
}
Swiper code:
public void Update()
{
swipedRight = false;
swipedLeft = false;
swipedUp = false;
swipedDown = false;
if (Input.touches.Length > 0)
{
Touch t = Input.GetTouch(0);
if (t.phase == TouchPhase.Began)
{
startPos = new Vector2(t.position.x / (float)Screen.width, t.position.y / (float)Screen.width);
startTime = Time.time;
}
if (t.phase == TouchPhase.Ended)
{
if (Time.time - startTime > MAX_SWIPE_TIME) // press too long
return;
Vector2 endPos = new Vector2(t.position.x / (float)Screen.width, t.position.y / (float)Screen.width);
Vector2 swipe = new Vector2(endPos.x - startPos.x, endPos.y - startPos.y);
if (swipe.magnitude < MIN_SWIPE_DISTANCE) // Too short swipe
return;
if (Mathf.Abs(swipe.x) > Mathf.Abs(swipe.y))
{ // Horizontal swipe
if (swipe.x > 0)
{
// isSwiping = true;
swipedRight = true;
}
else
{
// isSwiping = true;
swipedLeft = true;
}
}
else
{ // Vertical swipe
if (swipe.y > 0)
{
swipedUp = true;
}
else
{
swipedDown = true;
}
}
}
}
if (debugWithArrowKeys)
{
swipedDown = swipedDown || Input.GetKeyDown(KeyCode.DownArrow);
swipedUp = swipedUp || Input.GetKeyDown(KeyCode.UpArrow);
swipedRight = swipedRight || Input.GetKeyDown(KeyCode.RightArrow);
swipedLeft = swipedLeft || Input.GetKeyDown(KeyCode.LeftArrow);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612230446im_/https://answers.unity.com/themes/thub/images/avi.jpg)