Reputation: 129
I have an object, let's say for example an ObjectBoy. My object walks forward after I click on the terrain, but it is walking with it's back first, and it looks like it's walking backwards. I've researched in Unity forums that to overcome this, you must put the object in an Empty game object as it's parent and correct the orientation on the child. Now, everything works fine except the animation is not playing anymore, it just float on the terrain to go to it's destination. The solution I'm thinking to this was to access the animation in the child but I can't see anywhere if it's possible. Here's my code:
public class ClickToMove : MonoBehaviour {
NavMeshAgent navAgent;
Animation animation;
public AnimationClip runAnimation;
public AnimationClip idleAnimation;
void Start ()
{
navAgent = GetComponent<NavMeshAgent>();
animation = GetComponent<Animation>();
}
void Update ()
{
move();
animate();
}
void move()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Input.GetMouseButton(0))
{
if (Physics.Raycast(ray, out hit, 100))
{
navAgent.SetDestination(hit.point);
}
}
}
void animate()
{
if (navAgent.velocity.magnitude > 0.5f)
{
animation.CrossFade(runAnimation.name);
}
else
{
animation.CrossFade(idleAnimation.name);
}
}
}
Upvotes: 0
Views: 1045
Reputation: 3049
you can get a component that is in children with GetComponentInChildren
public Animation animation;
void Example() {
animation= GetComponentInChildren<Animation>();
}
Upvotes: 1