- Home /
Increase speed from 0f-10f over time
Hey Guys :) Maybe you can help me :) I want to increase the speed of an object from 0.0f to 10.0f over 1 minute (variable maybe i do 30 sec at least) without pressing anything so the speed increases when the game starts. I read so much solutions in this forum but nothing worked for me... or do that what i wanted. You can see my latest code down low. I really hope u can support me with ur experience ;)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
float currentSpeed = 0f;
float maxSpeed = 10f;
public float movementSpeed = 5.0f;
public GameObject player;
private float screenCenterX;
private void Start()
{
// save the horizontal center of the screen
screenCenterX = Screen.width * 0.5f;
}
private void Update()
{
//////////////// Not really worked /////////////////////////////////////////////////////////////////////////////
currentSpeed = Mathf.Lerp(currentSpeed, maxSpeed, Time.deltaTime);
transform.position -= transform.forward * currentSpeed * Time.deltaTime;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if there are any touches currently
if (Input.touchCount > 0)
{
// get the first one
Touch firstTouch = Input.GetTouch(0);
// if it began this frame
if (firstTouch.phase == TouchPhase.Began)
{
if (firstTouch.position.x > screenCenterX)
{
// if the touch position is to the right of center
// move right
StartCoroutine(RotateAround(Vector3.up, 90.0f, 0.35f));
}
else if (firstTouch.position.x < screenCenterX)
{
// if the touch position is to the left of center
// move left
StartCoroutine(RotateAround(Vector3.up, -90.0f, 0.35f));
}
}
}
else
{
transform.position -= transform.forward * Time.deltaTime * movementSpeed;
}
}
IEnumerator RotateAround(Vector3 axis, float angle, float duration)
{
float elapsed = 0.0f;
float rotated = 0.0f;
while (elapsed < duration)
{
float step = angle / duration * Time.deltaTime;
transform.RotateAround(transform.position, axis, step);
elapsed += Time.deltaTime;
rotated += step;
yield return null;
}
transform.RotateAround(transform.position, axis, angle - rotated);
}
}
Answer by Hellium · Apr 18, 2017 at 09:21 AM
Try something like this :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
float currentSpeed = 0f;
float maxSpeed = 10f;
public float movementSpeed = 5.0f;
public GameObject player;
private float screenCenterX;
// New variables :
public float accelerationTime = 60;
private float minSpeed ;
private float time ;
private void Start()
{
// save the horizontal center of the screen
screenCenterX = Screen.width * 0.5f;
minSpeed = currentSpeed;
time = 0 ;
}
private void Update()
{
// https://docs.unity3d.com/ScriptReference/Mathf.SmoothStep.html
currentSpeed = Mathf.SmoothStep(minSpeed, maxSpeed, time / accelerationTime );
transform.position -= transform.forward * currentSpeed * Time.deltaTime;
time += Time.deltaTime ;
// ....
}
Hey :) Yes that is what i want ! it increases the speed over the seconds i write down in the script ^^ This was very useful thank you for this code it helps a lot ^^
I know this is an old post but I was hoping you could still help me. I understand that "accelerationTime" is the duration which it takes to reach "maxSpeed" My question is how can I control the actual speeding up and the slowing down of the value?
currentSpeed = Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime );
time
tracks the time elapsed since the call to the Start method (since the start of the scene in most of the cases).
accelerationTime
is the time needed for the object to reach its full speed ( maxSpeed
)
When time
is equal to 0, time / accelerationTime
returns 0
, so Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime )
returns $$anonymous$$Speed
When time
is equal or greater than accelerationTime
, time / accelerationTime
returns a value greater than 1
, so Mathf.SmoothStep($$anonymous$$Speed, maxSpeed, time / accelerationTime )
returns maxSpeed
transform.position -= transform.forward * currentSpeed * Time.deltaTime;
Since the speed (velocity) of an object is calculated as follow: v = (position at t₂ - position at t₁ ) / (Δt)
, it's easy to deduce that position at t₂ = position at t₁ + vΔt
. Here v = -transform.forward * currentSpeed
(`v` is the velocity vector)
time += Time.deltaTime ;
Just keeps track of the timer
If you want the current speed to reach the value of maxSpeed
in a shorter duration, decrease accelerationTime
. If you want to keep the duration but influence how currentSpeed reaches the max speed, take a look at easing functions. Here, I've used Mathf.SmoothStep
, but you could have used Lerp
, or any other easing methods.
You can't with the smooth step function as it has a built-in s-shape curve (specifically the h01 hermit spline function). You can find the definition of SmoothStep over here.
Smoothstep is the same as using an ordinary Lerp but the t value put through the hermit polynomial so it does not grow linear but in an S shape. You are free to come up with a different function that maps the 0 to 1 range back to the 0 to 1 range but has your desired behaviour. Though that's kinda tricky.
The easiest solution would be to simply use an AnimationCurve that you can adjust in the editor to your liking. Just make sure the input and output range is 0 to 1 and you're good to go ^^.
public AnimationCurve speedRamp; // edit in the inspector
// [ ... ]
currentSpeed = Mathf.Lerp($$anonymous$$Speed, maxSpeed, speedRamp(time / accelerationTime));
Of course if you like you can simply drop the "/ accelerationTime" and now the animation curve would have seconds as input values. This may be useful if you want to describe a more complex speed behaviour over time. Keep in $$anonymous$$d that you can / should provide a reasonable wrap mode at the end of the curve since the time keeps increasing. Clamping the last value is usually what you want.
Thanks for your help. You put me on the right track. I'm going to test that out.