I would like to call a script whenever the object is moved / rotated. (Ideally in Editor and in Game)
Something like “OnMove()”… which doesn’t exist.
How would I go about it?
I would like to call a script whenever the object is moved / rotated. (Ideally in Editor and in Game)
Something like “OnMove()”… which doesn’t exist.
How would I go about it?
store the transform.position and transform.rotation as variables so each frame you know what the position and rotation was last frame.
you can then check for movement in Update() before updating the position and rotation for the current frame, if it has moved you call your “OnMove()” function.
at least that’s how i’d look at doing it
LeftyRighty: Thanks for the answer. That’s actually very simple and does what I want. Cool!
I just came across this, which does what I want and is already built in Unity: http://docs.unity3d.com/Documentation/ScriptReference/Transform-hasChanged.html
function OnUpdate () {
if (transform.hasChanged)
{
print("The transform has changed!");
transform.hasChanged = false;
}
}
Cool I did not know about this transform.hasChanged. Looks useful!
hadn’t seen that before either just be careful about it not checking if the variables have actually changed.
A call of
transform.position = transform.position;
would set hasChanged to true, but the position hasn’t actually changed.