Hello everyone. I’m trying to build a prototype where you swipe an object through a puzzle. The mechanic I’m trying to aim for is similar to that of the Ice Path in Pokemon Silver/Gold.
Here’s an exact example of what I’m trying to accomplish.
Right now, I’ve managed to get the swipe gesture working, but am having a hard time trying to get the mechanic working properly. I’m using a RigidBody with a velocity, but it’s just not doing what I want it to do. The object is either not fast enough and stops halfway along the path, or bounces off the object. Here is the script I’m using:
using UnityEngine;
using System.Collections;
public class SwipeMotionsMain : MonoBehaviour {
public float minSwipeDistY;
public float minSwipeDistX;
//public GUIText Swipe;
private Vector2 startPos;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
Ray ray = Camera.main.ScreenPointToRay (touch.position);
RaycastHit rayCastHit;
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if(Physics.Raycast ( ray, out rayCastHit )) {
if (rayCastHit.collider.gameObject.tag == "Player") {
rigidbody.isKinematic = false;
if ( swipeDistVertical > minSwipeDistY) {
float swipeValue = Mathf.Sign (touch.position.y - startPos.y);
if (swipeValue > 0) {
//Swipe.text = "Up Swipe"; //Up Swipe
rigidbody.velocity = new Vector3(0, 0, 20);
Debug.Log ("Player Up");
}
else if (swipeValue < 0) {
//Swipe.text = "Down Swipe"; //Down Swipe
rigidbody.velocity = new Vector3(0, 0, -20);
Debug.Log ("Player Down");
}
}
}
}
float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if(Physics.Raycast ( ray, out rayCastHit )) {
if (rayCastHit.collider.gameObject.tag == "Player") {
rigidbody.isKinematic = false;
if ( swipeDistHorizontal > minSwipeDistX) {
float swipeValue = Mathf.Sign (touch.position.x - startPos.x);
if (swipeValue > 0) {
//Swipe.text = "Right Swipe"; //Right Swipe
rigidbody.velocity = new Vector3(20, 0, 0);
Debug.Log ("Player Right");
}
else if (swipeValue < 0) {
//Swipe.text = "Left Swipe"; //Left Swipe
rigidbody.velocity = new Vector3(-20, 0, 0);
Debug.Log ("Player Left");
}
}
}
}
break;
}
}
}
}
I would greatly appreciate all the help I get. I will be checking back often to reply and help answer any questions.