- Home /
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:
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.
Did you get this figured out? I'm curious as well.
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