- Home /
Question by
Richard-Tec · Jun 12, 2019 at 09:42 PM ·
c#scripting problemcollisionrigidbody
The problem is that when the player (in this case a simple cube with a gameobject that contains a swipe script for its movement) collides with some other object in the game, the player begins to shake.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Swipe : MonoBehaviour { private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown; private bool isDraging = false; private Vector2 startTouch, swipeDelta;
private void Update()
{
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
#region Standalone Inputs
if (Input.GetMouseButtonDown(0))
{
tap = true;
isDraging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isDraging = false;
Reset();
}
#endregion
#region Mobile Inputs
if(Input.touches.Length > 0)
{
if(Input.touches[0].phase == TouchPhase.Began)
{
isDraging = true;
tap = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDraging = false;
Reset();
}
}
#endregion
// Calculate the distance
swipeDelta = Vector2.zero;
if(isDraging)
{
if (Input.touches.Length > 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
// Did we cross the deadzone?
if (swipeDelta.magnitude > 125)
{
// Wich direction?
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)
swipeDown = true;
else
swipeUp = true;
}
Reset();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDraging = false;
}
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
}
Comment
Your answer
Follow this Question
Related Questions
Collision detection from specific directions using rigid body character? 0 Answers
Rigidbody trapped in collider after collision 1 Answer
Check if other object has fired a Collision Event 2 Answers
How can you make an object visible in Unity when you jump on it? 0 Answers
Force a script generated primitive to test for collision 2 Answers