Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abhipatel · Mar 22, 2014 at 08:22 AM · androidforceswipe

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);
         }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image abhipatel · Mar 22, 2014 at 09:56 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges