• 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
Question by horus developer02 · Apr 04, 2014 at 09:51 AM · c#movementpositionacceleration

Spaceship movement with acceleration and deceleration on Unity

I've been trying for a long time to make a code that simulates a spaceship movement with acceleration and deceleration (based on Time.deltaTime) from one point to another and I can't figure out how to do it. I mean, I've been able to do the acceleration part but, I really don't know how to make the deceleration...

Here's an example image of what I want:

alt text

At start, I though about using Vector3.Lerp with an Ease function but then, I realized that it would not work because the end point is not fixed. The end point is related to the position of the finger of the player (because it is to work like a drag system).

This is the code that I have so far that calculates the direction Vector (already with speed) to apply to the current spaceship position:

 protected float DistanceSmoothX(float current_x){
     last_dir_x = new_dir_x;
     //New direction
     if (current_x==0)
         new_dir_x=0;
     else if (current_x>0)
         new_dir_x=1;
     else
         new_dir_x=-1;
     
     //If direction changed, update increment based on the old and new directions.
     if (new_dir_x!=last_dir_x){
         //Debug.Log (current_x);
         if (new_dir_x-last_dir_x<0)
             inc_x=-1.0f/spaceship.ease_time;
         else
             inc_x=1.0f/spaceship.ease_time;
     } else if (current_v_x>current_x){
         inc_x=-1.0f/spaceship.ease_time;
     } else if (current_v_x<current_x){
         inc_x=1.0f/spaceship.ease_time;
     }
     
     //Calculate the accumulated increment
     current_v_x+=inc_x*Time.deltaTime;
     
     //Verify if it doesn't pass the new_dir_x
     if (inc_x>0 && current_v_x>current_x)
         current_v_x=current_x;
     else if (inc_x<0 && current_v_x<current_x)
         current_v_x=current_x;
     current_v_x= Mathf.Clamp(current_v_x,-1,1);
     
     last_x = current_x;
     last_speed_x=current_v_x*spaceship.speed;
     //Multiply new increment to the passed current_x
     return last_speed_x*Time.deltaTime;
 }
 
 protected float DistanceSmoothY(float current_y){
     last_dir_y = new_dir_y;
     //New direction
     if (current_y==0)
         new_dir_y=0;
     else if (current_y>0)
         new_dir_y=1;
     else
         new_dir_y=-1;
     
     //If direction changed, update increment based on the old and new directions.
     if (new_dir_y!=last_dir_y){
         if (new_dir_y-last_dir_y<0)
             inc_y=-1.0f/spaceship.ease_time;
         else
             inc_y=1.0f/spaceship.ease_time;
     } else if (current_v_y>current_y){
         inc_y=-1.0f/spaceship.ease_time;
     } else if (current_v_y<current_y){
         inc_y=1.0f/spaceship.ease_time;
     }
     
     //Calculate the accumulated increment
     current_v_y+=inc_y*Time.deltaTime;
     
     //Verify if it doesn't pass the new_dir_x
     if (inc_y>0 && current_v_y>current_y)
         current_v_y=current_y;
     else if (inc_y<0 && current_v_y<current_y)
         current_v_y=current_y;
     current_v_y= Mathf.Clamp(current_v_y,-1,1);
     
     last_y = current_y;
     last_speed_y=current_v_y*spaceship.speed;
     //Multiply new increment to the passed current_x
     return last_speed_y*Time.deltaTime;
 }

So, this works well for the acceleration part and also for the constant speed part. Now, how can I make the deceleration? I tried to calculate the stop distance based on **[time (final_velocity + initial_velocity)/2]* but it did not work as I expected... Any clues of what can I do? Should I do my acceleration algorithm differently in order to be able to make the deceleration more easily?

I'm using Unity 4.3.4 and my code is all written in C#.

I'm also interested in different approaches to do this.

trajectory.png (50.7 kB)
Comment

People who like this

0 Show 2
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 aitchest-of-dees · Aug 05, 2014 at 09:15 PM 0
Share

Did you get this figured out? I'm curious as well.

avatar image DemSec · Jul 14, 2016 at 03:41 AM 0
Share

I also need to know this math, but in my instance, it's for rotational torque:

http://answers.unity3d.com/questions/1213141/move-towards-angle-with-addtourque.html

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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 avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Player should slow down linearly on release 1 Answer

[C#] Accelerate then decelerate to its wanted position 1 Answer

How can i make my enemy jump with a delay 1 Answer

Distribute terrain in zones 3 Answers


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