- Home /
How can I animate an object to my proximity and movement?
I'm not sure the best way to explain this, but this Simpsons clip actually shows what I'm trying to achieve quite well:
http://www.youtube.com/watch?v=G5CZ58CBGtg
I am trying to animate an object in a similar way to how the sun reacts to Homer's movement. In my Unity scene, Homer would be the FPS Controller and the sun would just be a cube (to keep it simple).
I'm basically trying to make it so when you walk closer to the cube it goes up in the Y-axis (until it has reached where I want it to stop) and when you walk away it goes down. If you are not moving, neither should the object
I'm new to Unity and don't know any scripting so any starting point would be helpful
Get the players X/Z axis (Depending on where the sun is), make the sun equal to the Y and X/Z axis of the sun (Again, depending on where the sun is). This could work roughly...
Answer by Statement · Apr 08, 2011 at 01:14 PM
A simple solution would be to create a script that measure distance to the player, and lerp position for the sun. The sun could be made a child of the object, placed in 0, 0, 0 local. When you get closer in x/z, the sune rise in y. If you want to measure distance in all axes, then just remove a.y = b.y;
var player : Transform; // Target your player var sun : Transform; // Target your sun var range = 10.0f; var sunHeight = 5.0f
function Update() { var a = player.position; var b = transform.position; a.y = b.y; // Optional var distance = Vector3.Distance(a, b); var percent = Mathf.Clamp01(distance / range); sun.localPosition = Vector3.Lerp(Vector3.up * sunHeight, Vector3.zero, percent); }
Thanks for the quick reply! Will give this a go soon and let you know how I get on