- Home /
Move GameObject with Swipe and when Swipe Ended Stop Gameobject Slowly
Hello..Friends...I am New in unity I am worked on android 2d unity game....but i face some problem with Playermovement script as follows.....
Here is My 'Pooh.cs' Script in this script when TouchPhase.Moved then Add Force to Gameobject and while TouchPhase.Ended I remove all forces but it can't remove and Player continue to move...Please Help me...
using UnityEngine;
using System.Collections;
public class Pooh : MonoBehaviour
{
private float length = 0;
private bool SW = false;
private Vector3 final;
private Vector3 startpos;
private Vector3 endpos;
public GameObject player;
public float coolDown = 2f;
Vector2 touchDeltaPosition;
float dis = 0;
private Vector3 tmp;
// Use this for initialization
string ha;
float temp;
Vector2 temp1;
float timer;
Vector3 v1, v2, v3, v4 ;
float p;
void Start ()
{
}
// Update is called once per frame
void FixedUpdate ()
{
SW = false;
length = 0;
dis = 0;
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) {
final = Vector3.zero;
length = 0;
SW = false;
touchDeltaPosition = Input.GetTouch (0).position;
startpos = new Vector3 (touchDeltaPosition.x, touchDeltaPosition.y, 0);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved) {
SW = true;
dis = Mathf.Floor (Vector3.Distance (Input.GetTouch (0).position, startpos));
if (SW) {
Vector2 touchPosition = Input.GetTouch (0).position;
endpos = new Vector3 (touchPosition.x, touchPosition.y, 0);
final = endpos - startpos;
length = final.magnitude;
if (Mathf.Abs (final.x) > Mathf.Abs (final.y)) {
if (final.x > 0) {
//Swipe (SwipeDirection.Right);
v1 = new Vector3 (1f, 0f, 0f);
player.rigidbody2D.AddForce (v1);
player.transform.Translate (new Vector3 (length / 1500, 0, 0));
} else {
//Swipe (SwipeDirection.Left);
v2 = new Vector3 (-1f, 0f, 0f);
player.rigidbody2D.AddForce (v2);
player.transform.Translate (new Vector3 (-length / 1500, 0, 0));
}
} else {
if (final.y > 0) {
//Swipe (SwipeDirection.Up);
v3 = new Vector3 (0f, 1f, 0f);
player.rigidbody2D.AddForce (v3);
player.transform.Translate (new Vector3 (0, length / 1500, 0));
} else {
//Swipe (SwipeDirection.Down);
v4 = new Vector3 (0f, -1f, 0f);
player.rigidbody2D.AddForce (v4);
player.transform.Translate (new Vector3 (0, -length / 1500, 0));
}
}
}
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Canceled) {
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary) {
SW = false;
length = 0;
dis = 0;
touchDeltaPosition = Input.GetTouch (0).position;
startpos = new Vector3 (touchDeltaPosition.x, touchDeltaPosition.y, 0);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended) {
SW = true;
dis = Mathf.Floor (Vector3.Distance (Input.GetTouch (0).position, startpos));
if (SW) {
Vector2 touchPosition = Input.GetTouch (0).position;
endpos = new Vector3 (touchPosition.x, touchPosition.y, 0);
final = endpos - startpos;
if (final.x > 0) {
//Swipe (SwipeDirection.Right);
v1 = new Vector3 (0f, 0f, 0f);
player.rigidbody2D.AddForce (v1);
} else {
//Swipe (SwipeDirection.Left);
v2 = new Vector3 (0f, 0f, 0f);
player.rigidbody2D.AddForce (v2);
}
} else {
if (final.y > 0) {
//Swipe (SwipeDirection.Up);
v3 = new Vector3 (0f, 0f, 0f);
player.rigidbody2D.AddForce (v3);
} else {
//Swipe (SwipeDirection.Down);
v4 = new Vector3 (0f, 0f, 0f);
player.rigidbody2D.AddForce (v4);
}
}
}
}
void OnGUI ()
{
//GUI.Box (new Rect (50, 300, 300, 30), " " + timer);
}
}
Answer by whydoidoit · Mar 22, 2014 at 08:30 AM
You are just adding a force of 0, that's not doing anything at all.
You want to set the velocity of the rigidbody to Vector3.zero to stop it.
that's right.. buddy....!!! but i want to stop it slowly.. not at a time of TouchPhase.Ended but after 2-3 second. for Ended....
Your answer
Follow this Question
Related Questions
Swipe and Joystick Together on Mobile 0 Answers
Android wear swipe to dismiss 0 Answers
Swipe & gesture controls for android 1 Answer
Disable Swipe Controls For Limited Time? 1 Answer
projectile speed and Direction different in some devices 1 Answer