- Home /
I`m not able to jump
Hi everyone. I followed this YouTube tutorial (link text) to get touch inputs for my game but for some reason I am not able to jump. Can anybody help me how to fix it?`enter code here`
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MobileInput : MonoBehaviour {
private const float DeadZone = 100.0f;
public static MobileInput Instance { set; get; }
private bool tap, swipeLeft, swipeRight, swipeUp;
private Vector2 swipeDelta, startTouch;
public bool Tap { get { return tap; } }
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
private void Awake()
{
Instance = this;
}
private void Update()
{
// Reseting booleans
tap = swipeLeft = swipeRight = swipeUp = false;
// Input
if (Input.touches.Length != 0)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
tap = true;
startTouch = Input.mousePosition;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
startTouch = swipeDelta = Vector2.zero;
}
}
// Calculate Distance
swipeDelta = Vector2.zero;
if (startTouch != Vector2.zero)
{
// Check with mobile
if(Input.touches.Length != 0)
{
swipeDelta = Input.touches[0].position - startTouch;
}
}
// Check if you are beyond the deadzone
if (swipeDelta.magnitude > DeadZone)
{
// Confirmed Swipe
float x = swipeDelta.x;
float y = swipeDelta.y;
if(Mathf.Abs(x) > Mathf.Abs(y))
{
// Left or Right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else
{
//Up or Down
if (y > 0)
swipeUp = true;
}
startTouch = swipeDelta = Vector2.zero;
}
}
}
If that doesn´t help you I could also send my player controller script. You just need to ask. Thanks in advance!
I also added an animation which works when I swipe up but the player is not jumping.
Answer by ray2yar · Jul 08, 2020 at 12:04 PM
If it's detecting the action in the animator then your problem is likely in whatever script controls the actual character movement. Don't see any code here that would change a character's position.
private float jumpForce **strong text**= 2.0f;
private float gravity = 8.0f;
private float verticalVelocity;
if(isGrounded)
{
verticalVelocity = -0.1f;
if ($$anonymous$$obileInput.Instance.SwipeUp)
{
verticalVelocity = jumpForce;
anim.SetTrigger("Jump");
}
else
{
verticalVelocity -= (gravity * Time.deltaTime);
}
Here is my player jumping code.
Your answer
Follow this Question
Related Questions
My jump code seems to be broken, the character's flags for jumping appear to be working strangely. 1 Answer
How to add collider to a Torus object 5 Answers
Problems with the instantiation of a GameObject C# 1 Answer
NullReferenceException: Object reference not set to an instance of an object ? 1 Answer