# how can i detect if an object is accelerating or decelerating?

something like this :

if(accelerating){
//code
}

if(decelerating){
//code
}
if(still){
//code
}

As indicated by @TheJimz, you can compute the velocity and acceleration each frame :

``````    private Vector3 lastPosition ;
private Vector3 lastVelocity;
private Vector3 lastAcceleration;

private void Awake()
{
Vector3 position = transform.position;
Vector3 velocity = Vector3.zero;
Vector3 acceleration = Vector3.zero;
}

private void Update()
{
Vector3 position = transform.position;
Vector3 velocity = (position - lastPosition) / Time.deltaTime;
Vector3 acceleration = (velocity - lastVelocity) / Time.deltaTime;

if( Mathf.Abs(acceleration.magnitude - lastAcceleration.magnitude ) < 0.01f )
{
// Still
}
else if( acceleration.magnitude > lastAcceleration.magnitude )
{
// Accelerating
}
else
{
// Decelerating
}

lastAcceleration = acceleration;
lastVelocity = velocity;
lastPosition = position;
}
``````

well… you could do:

``````Rigidbody rb = TestGameObject.GetComponent<Rigidbody>();

if(rb.velocity.x > MaxVel|| rb.velocity.y > MaxVel || rb.velocity.z > MaxVel)
{
Its accelerating
}
``````

and use that to detect if its decelerating.

You would have to compare velocities between frames, with a positive change meaning it’s accelerating.