- Home /
How to play an animation with waypoints
Hi there I am creating an underwater game, and I have set up a waypoint system for a fish model. The waypoint script works okay, but the fish is facting the wrong direction when it moves. So I added a simple animation to it, now it does not move. How can I have the fish move in the waypoint system and still have the animation play?
Here is my script: var waypoints : Transform[]; private var current_waypoint : int = 0; var speed : int = 10; function Update () { var target: Vector3 = waypoints[current_waypoint].position; var moveDirection : Vector3 = target - transform.position; if(moveDirection.magnitude < 2) { current_waypoint++; if(current_waypoint >= waypoints.length) { current_waypoint = 0; } } else { rigidbody.velocity = moveDirection.normalized * speed; } transform.LookAt(waypoints[current_waypoint]); }
Answer by GameVortex · Dec 06, 2013 at 10:43 AM
If all you want is to have the fish face the correct direction you could make a new GameObject that will be the root, add your current fish gameobject as a child to that root GameObject and move the fish component to the root instead. Now you can rotate the Fish GameObject to face the correct direction while the Fish component moves the root itself.
If you still want to use an animation, just do the exact same thing as above, but have the animation play on the Fish GameObject while the Fish Component moves the Root. This way the animation does not overwrite the transform changes you do in the Fish script.
Thanks! I am still a little confused though, what do you meant by root?
Sorry, I was rambling a little. =P Root is just the name of the new GameObject. It does not have to be named Root though.
Step by step:
Create GameObject named "Root".
Add Fish GameObject as child of Root.
$$anonymous$$ove Fish Component from Fish GameObject to Root GameObject.
Rotate Fish GameObject so it faces the correct way.
Oh okay, but the fish is only made from one model, there is no children on the fish model. What do you mean by fish component? Sorry hah I am still new :(
Yay! Don't worry I have got it to work! Thanks so much for the help!!