- Home /
check if object is moving
Hi!
I use iTween a lot. But is there a way to check if an object is currently standing still or is moving?
Answer by Vonni · Jan 17, 2013 at 02:14 PM
Is the object a rigidbody? Or some object you might set into motion yourself? If it's a rigidbody you might be able to check if its "sleeping" or not. Else if you issued the movement, you might be able to predetermine whether its in motion or not with some clever scripting. Last solution would be to have a function that checks:
function IsThisObjectMoving (object : Transform) : boolean {
var sek0pos : Vector3;
var sek1pos : Vector3;
sek0pos = object.position;
yield(WaitForSeconds(1.0); // Wait one second
sek1pos = object.position;
if(sek1pos - sek0pos).sqrMagnitude > 0.1)
return true;
else
return false;
}
Answer by Jeffom · Jan 17, 2013 at 01:55 PM
well you could make something like this
ObjectA : Monobehaviour
{
public bool bIsOnTheMove = false;
private IEnumerator CheckMoving()
{
Vector3 startPos = transform.Position;
yield return new WaitForSeconds(1f);
Vector3 finalPos = transform.Position;
if( startPos.x != finalPos.x || startPos.y != finalPos.y
|| startPos.z != finalPos.z)
bIsOnTheMove = true;
}
}
you can use a while to keep the checking true, but it would be wise to only turn use the coroutine when you need to check the object. *this may not compile it's just a pseudo code to show the point i'm trying to make
Answer by JDSaraiva · Jun 06, 2014 at 10:48 AM
I came up with this solution, worked best in my case.
function calcSpeed() {
prevPos = personagem.transform.position;
yield WaitForSeconds(0.5);
actualPos = personagem.transform.position;
if (prevPos == actualPos) print("character stoped");
if (prevPos != actualPos) print("character on the move");
}
Answer by vikitosss · May 08, 2016 at 02:17 PM
jdsaraiva +1. I like this...
var IsMoved = false;
function moveCheck() {
var p1 = transform.position;
yield WaitForSeconds(0.5);
var p2 = transform.position;
IsMoved = (p1 != p2);
// or : IsMoved = (p1 == p2);
}