- Home /
how can i detect if an object is accelerating or decelerating?
something like this :
if(accelerating){ //code }
if(decelerating){ //code } if(still){ //code }
If you have a RigidBody, you can use the RigidBody.velocity. Store the value in each update and check, whether the velocity increased or decreased compared to the last update.
Answer by Hellium · Aug 21, 2017 at 03:03 PM
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;
}
Answer by shinevision · Aug 16, 2017 at 07:48 PM
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.
Answer by TheJimz · Aug 16, 2017 at 09:17 PM
You would have to compare velocities between frames, with a positive change meaning it's accelerating.
Your answer

Follow this Question
Related Questions
Get inpact location OnCollisionEnter? (c#) 2 Answers
Problem with time !! help me 1 Answer
Rotate object from localRotation A to localRotation B at rate C. 2 Answers
Collider problems with Speed 1 Answer