- Home /
gradually moving an object up to speed rather then instantly?
i want to know how to, on button press, make an object move without instantly getting to the speed i set it to move at. right now im using this script
function Update () {
if (Input.GetKey ("left"))
transform.position.x = transform.position.x + -0.10;
if (Input.GetKey ("right"))
transform.position.x = transform.position.x + 0.10;
}
which works, but when the button is pressed the object instantly moves at 0.10, i would like it to accelerate to 0.10 then keep moving at that, then when the button is released de-accelerate back to not moving. i hope this makes sense :/
Answer by IJM · Oct 11, 2010 at 01:20 AM
This code will work for you:
var Speed : float = 0;//Don't touch this var MaxSpeed : float = 10;//This is the maximum speed that the object will achieve var Acceleration : float = 10;//How fast will object reach a maximum speed var Deceleration : float = 10;//How fast will object reach a speed of 0
function Update () { if ((Input.GetKey ("left"))&&(Speed < MaxSpeed)) Speed = Speed - Acceleration Time.deltaTime;
else if ((Input.GetKey ("right"))&&(Speed > -MaxSpeed)) Speed = Speed + Acceleration Time.deltaTime; else { if(Speed > Deceleration Time.deltaTime) Speed = Speed - Deceleration Time.deltaTime; else if(Speed < -Deceleration Time.deltaTime) Speed = Speed + Deceleration Time.deltaTime; else Speed = 0; }
transform.position.x = transform.position.x + Speed * Time.deltaTime; }
hi your code is awesome i am trying to maintain $$anonymous$$imum speed but when i increase speed it is floating to 0 and it is increasing again can you please help me
Can anybody please explain the logic behind this ? Why is Speed = Speed - Acceleration Time.DeltaTime ?
What is operator between Accleration and Time.DeltaTime ? Please help
It should be Speed = Speed - (Acceleration * Time.DeltaTime)
.
Answer by dreg_master · Jan 12, 2017 at 10:38 AM
Dude, you are a legend sir. I spend Days trying to get this to work with a script 3 pages long and still pulling out hair. Your solution was in Java i converted into c# and it worked marvelously. Thank you. here is what worked for me in C#
using UnityEngine; using System.Collections;
public class test2 : MonoBehaviour { public Vector3 position; float Speed = 0; //Don't touch this float MaxSpeed = 10; //This is the maximum speed that the object will achieve float Acceleration = 10; //How fast will object reach a maximum speed float Deceleration = 10; //How fast will object reach a speed of 0 // Use this for initialization void Start() { transform.position = position; }
// Update is called once per frame
void Update()
{
if ((Input.GetKey("left")) && (Speed < MaxSpeed)) Speed = Speed - Acceleration * Time.deltaTime;
else if ((Input.GetKey("right")) && (Speed > -MaxSpeed)) Speed = Speed + Acceleration * Time.deltaTime;
else
{
if (Speed > Deceleration * Time.deltaTime) Speed = Speed - Deceleration * Time.deltaTime;
else if (Speed < -Deceleration * Time.deltaTime) Speed = Speed + Deceleration * Time.deltaTime;
else
Speed = 0;
}
position.x = transform.position.x + Speed * Time.deltaTime;
transform.position = position;
}
}
Answer by Ray-Pendergraph · Oct 11, 2010 at 12:57 AM
Try lerping it, here is the documentation:
transform.position = Vector3.Lerp(start.position, end.position, Time.time);
This gradually moves between two points. The movement is constant over time, to have the movement gradual (like building momentum) at the start and stop points I believe you must call lerp with the current position like so:
transform.position = Vector3.Lerp(transform.position, end.position, Time.time);
Yes, actually just look at the second example on that link. :-). Duh.
Answer by ChimeraPriest · Nov 11, 2015 at 01:16 AM
For 2D platformer movement with some acceleration and deceleration...
void Update()
{
float h = Input.GetAxis("Horizontal");
Rigidbody2D rbody = GetComponent<Rigidbody2D>();
rbody.velocity = Vector3.right * h * speed;
}
Answer by tjBlueBeast · Dec 09, 2015 at 09:48 AM
public float speed = 0f;
public float maxSpeed = 10f;
public float forceMagnitude = 2f;
void FixedUpdate() {
// Store the input axes.
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
// Move the player around the scene.
Move (h, v);
//other stuff...
}
void Move(float h, float v) {
if (v != 0 || h != 0) {
// Set the movement vector based on the axis input.
movement.Set (h, 0f, v);
speed = Mathf.Min(speed + forceMagnitude * Time.deltaTime, maxSpeed);
} else {
speed = Mathf.Max(speed - forceMagnitude * Time.deltaTime * 1.5f, 0);
}
//Debug.Log (speed);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
playerRigidbody.MovePosition(transform.position + movement);
}