Hey,
im new into Unity and coding and I wrote this code. It should let the player jump, if you swipe up (vertical) on a mobile device.
Why does this not work?
Sry for my bad english, if there are any mistakes. Im german.
An answer would be very great.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Touch_Input : MonoBehaviour
{
public float jumpForce;
public float maxSwipeTime;
public float minSwipeTime;
public float MinSwipeDistance;
private float SwipeTime;
private float SwipeLength;
private float SwipeStartTime;
private float SwipeEndTime;
private Rigidbody2D rb;
private Vector2 SwipeStartPos;
private Vector2 SwipeEndPos;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void swipeTest()
{
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
SwipeStartTime = Time.time;
SwipeStartPos = touch.position;
}
else if (touch.phase == TouchPhase.Ended)
{
SwipeEndTime = Time.time;
SwipeEndPos = touch.position;
SwipeTime = SwipeEndTime - SwipeStartTime;
SwipeLength = (SwipeEndPos - SwipeStartPos).magnitude;
if (SwipeTime < maxSwipeTime && SwipeLength > MinSwipeDistance)
{
SwipeControl();
}
}
}
}
void SwipeControl()
{
Vector2 Distance = SwipeEndPos - SwipeStartPos;
float xDistance = Mathf.Abs(Distance.x);
float yDistance = Mathf.Abs(Distance.y);
float xDistance = Distance.x;
float yDistance = Distance.y;
if (Distance.x < Distance.y)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
} }