- Home /
Swipe movement limitation
Hi guys, i'd like to move my player with rigidbody2d by swipe left and right, and i'd like to move it into a specific point, i saw the api mathf.clamp to create the movement limitations, but i don't know how to create multiple limitations
I post a gif to make you understand better:
using UnityEngine;
using System.Collections;
public class Gestures : MonoBehaviour {
public Rigidbody2D playerRigibody2D;
public Transform playerTransform;
public float speed;
public Vector2 movementRight;
public Vector2 movementLeft;
private GameController gamecontroller;
void Start ()
{
GameObject GameControllerObject = GameObject.FindWithTag ("GameController");
if (GameControllerObject != null) {
gamecontroller = GameControllerObject.GetComponent <GameController> ();
}
if (gamecontroller == null) {
Debug.Log ("Cannot find 'GameController' script ");
}
}
void OnEnable(){
EasyTouch.On_SwipeEnd += On_SwipeEnd;
}
void OnDestroy(){
EasyTouch.On_SwipeEnd -= On_SwipeEnd;
}
void On_SwipeEnd (Gesture gesture)
{
switch (gesture.swipe) {
case EasyTouch.SwipeDirection.DownLeft:
case EasyTouch.SwipeDirection.UpLeft:
case EasyTouch.SwipeDirection.Left:
playerRigibody2D.rigidbody2D.velocity = movementLeft * speed;
break;
case EasyTouch.SwipeDirection.DownRight:
case EasyTouch.SwipeDirection.UpRight:
case EasyTouch.SwipeDirection.Right:
playerRigibody2D.rigidbody2D.velocity = movementRight * speed;
break;
}
}
void FixedUpdate ()
{
}
}
Comment
Best Answer
Answer by Ciuino · Mar 16, 2015 at 05:13 PM
I used mathf.lerp and mathf.clamp functions
using UnityEngine;
using System.Collections;
public class PlayerMoviment_Lerp : MonoBehaviour {
public float smooth;
public float distance;
private Vector3 newPosition;
void Awake ()
{
newPosition = transform.position;
}
void OnEnable(){
EasyTouch.On_SwipeEnd += On_SwipeEnd;
}
void OnDestroy(){
EasyTouch.On_SwipeEnd -= On_SwipeEnd;
}
void On_SwipeEnd (Gesture gesture)
{
switch (gesture.swipe) {
case EasyTouch.SwipeDirection.DownLeft:
case EasyTouch.SwipeDirection.UpLeft:
case EasyTouch.SwipeDirection.Left:
newPosition += Vector3.left * distance;
break;
case EasyTouch.SwipeDirection.DownRight:
case EasyTouch.SwipeDirection.UpRight:
case EasyTouch.SwipeDirection.Right:
newPosition += Vector3.right * distance;
break;
}
}
void Update ()
{
PositionChanging();
transform.position = new Vector3 (Mathf.Clamp (transform.position.x,-2.0f,2.0f),-8.51f,0.0f);
newPosition = new Vector3 (Mathf.Clamp (newPosition.x, -2.0f,2.0f),-8.51f,0.0f);
Debug.Log (newPosition);
}
void PositionChanging ()
{
transform.position = Vector3.Lerp(transform.position, newPosition, smooth * Time.deltaTime);
}
}
Answer by fafase · Feb 25, 2015 at 05:25 PM
You have 5 possible positions from -2 to 2 but those could be stored as positions in an array.
You get the direction of your swipe so that you can convert it to -1 or 1.
Then you have an index that is your current position:
int [] array = {-2, -1, 0, 1, 2};
int value = GetSwipe();
index += value;
index = Mathf.Clamp(index, 0 , array.Length - 1);
int position = array[index];