• 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 tom-moon · Jun 08, 2017 at 03:34 PM · 2dvector3vector2movetowardsvector3.movetowards

conserve momentum of vector3.moveTowards in 2D game

I have a character who is hanging in mid air with a gravitational force constantly pulling them downwards. however, when a mouse click is detected, the character accelerates towards the click. I have written the following code.

 public class controller : MonoBehaviour {
     private Vector3 target;
     Vector3 velocity = Vector3.zero;
     public Vector3 gravity;
 
     float startTime;
     float elapsedTime;
 
     bool gravOn = true;
     // Use this for initialization
     void Start () {
         startTime = Time.time;
     }
 
     // Update is called once per frame
     void FixedUpdate () {
 
         if (gravOn == true) {
             elapsedTime = Time.time - startTime;
             velocity += gravity * elapsedTime;
             transform.position += velocity * elapsedTime;
         }
 
         if ((Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began) || (Input.GetMouseButton (0))) {
 
             gravOn = false;
 
             target = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             target.z = transform.position.z;
             transform.position = Vector3.MoveTowards (transform.position, target, 0.5f);
 
             startTime = Time.time;
 
         } else {
             gravOn = true;
         }
     }
 }

my problem is, when I release the mouse the character instantly stops accelerating towards the mouse click and instantly starts falling again. I would like the momentum of the acceleration caused by vector3.movetowards to be conserved, so the character could be "thrown" by the moveTowards function. What am i missing here?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity2D - MoveTowards not working as planned 1 Answer

Smoothly moving a 2D gameobject along a vector path 1 Answer

Should I use Vector3D in a 2D game? 1 Answer

My vector2.movetowards is 2-3x faster when trigger off and trigger on collider collides 0 Answers

How to use 2D pathfinding with vector 2 0 Answers

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