• 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 Virdari · Aug 13, 2014 at 09:52 PM · 2drigidbody2dvelocityswipe

issue with moving GameObject consistently

So here is what i'm trying to do: MainPlayer will run upwards endlessly, when there is a swipe from left to right or right to left he will switch lanes. Here is the code for mainplayer:

  using UnityEngine;
 using System.Collections;
 
 public class CharTouchCont: MonoBehaviour
 {
     public float moveSpeed = 3f;
     public float swipeVelocity = 9f;
     public float timeThreshold = 0.35f;
     // swipe related variables
     private bool canSwipe;
     private Vector2 startPosition; 
     private Vector2 endPosition;
     void Start ()
     {
         canSwipe = true;
     }
     
     void Update ()
     {
         rigidbody2D.velocity = new Vector2 (0, moveSpeed);
         if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Ended && canSwipe == true) // End of Touch
         {
             canSwipe = false; 
             endPosition = Input.GetTouch (0).position;
             if (startPosition.x < endPosition.x) // left to right swipe
             {
                 rigidbody2D.velocity = new Vector2 (swipeVelocity, rigidbody2D.velocity.y);
                 StartCoroutine ("delay");
             }
             else if (endPosition.x < startPosition.x) // right to left swipe
             { 
                 rigidbody2D.velocity = new Vector2 (-swipeVelocity, rigidbody2D.velocity.y);
                 StartCoroutine ("delay");
             }
         }
         if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Began && canSwipe == true) // Start of Touch
             startPosition = Input.GetTouch (0).position;
         if (Input.GetKey (KeyCode.Escape))
             Application.Quit ();
 
     }
     IEnumerator delay ()
     {
         yield return new WaitForSeconds (timeThreshold);
         rigidbody2D.velocity = new Vector2 (0, rigidbody2D.velocity.y);
         canSwipe = true;
     }
 }

i'm trying to move him to left or right by increasing the velocity in x axis by 9 for 0.35 seconds and then setting it back to 0. so i will move left to right depending on swipe by 3.15 units ( 9 * 0.35) the issue is that after i run the game my character starts to stop at positions which are slightly to the left or right of where he should be or sometimes in between 2 lanes. How do i make it so that my GameObject will move 3.15 units to left or right consistently?

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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Rigidbody2D.velocity out of controll 3 Answers

Need help understanding advise 1 Answer

2D Geometry dash-like ship physics 0 Answers

Rigidbody2D velocity changes when moving through a platform collider. 1 Answer

Movement Relative to Rotated Parent 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges