- Home /
How do I check if a non rigidbody object is moving or not
How would I check if an object (not rigidbody) is moving?
It doesnt matter what axis its moving along, just need to know if the velocity is greater than zero?
$$anonymous$$aybe something like this -
Vector3 currentPosition;
bool moving = false;
void Start () {
currentPosition = transform.position;
}
void Update () {
if(transform.position != currentPosition){
moving = true;
if(moving == true)
{
print ("$$anonymous$$oving");
}
} else {
moving = false;
if(moving == false)
{
print ("Not $$anonymous$$oving");
}
}
}
void LateUpdate(){
currentPosition = transform.position;
}
I changed your script slightly (basically just printing if the object is moving or not)
var currentPosition : Vector3;
var moving : boolean = false;
function Start ()
{
currentPosition = transform.position;
}
function Update()
{
if(transform.position != currentPosition)
{
moving = true;
currentPosition = transform.position;
if(moving == true)
{
print ("$$anonymous$$oving");
}
}
else
{
moving = false;
if(moving == false)
{
print ("Not $$anonymous$$oving");
}
}
}
But I've noticed that it says "Not $$anonymous$$oving" even when the object obviously is? The check box in the inspector confirms the object is moving but my print statement says not.
Is the script wrong, or have I made a mistake in the changes I made ???
Your code is just fine. I was just pointing you out a direction. And you just copy-paste it almost ;), Just measure the currentPosition in LateUpdate(). It will get you there. Edited my above comment according to your codes. But be warned, very swift tiny movement may not be detected. So, if it is ok with you, try the above codes. Hope it helps.
Answer by kartikeya · Dec 04, 2017 at 02:31 PM
Two variables, Holding last position and current one. Compare both of them inside "condition".
Your answer
Follow this Question
Related Questions
How to determine the axis specific speed (or velocity) of a kinematic object? 0 Answers
Issue regarding Rigidbody2D 0 Answers
Check CharacterController's Velocity 0 Answers
Can an object to transfer to another as it increases your speed? 1 Answer
How do I determine mouse speed on the x and z axis? 0 Answers