Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Score, Enemies, and Game State Create Enemy Navigation and AI

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

[SOLVED] The Bird walks the same path each time before sticking to idle state.

Hi all,

I feel like I've followed the video perfectly but something is clearly not right because when I play the game bird always walks slowly to the original position of the frog but then goes to its idle state and stays there.

Here's my birdMovement.cs code. Thanks :)

using UnityEngine;
using System.Collections;

public class BirdMovement : MonoBehaviour {

    [SerializeField]
    private Transform target;
    private NavMeshAgent birdAgent;
    private Animator birdAnimator;

    // Use this for initialization
    void Start () {
        birdAgent = GetComponent<NavMeshAgent> ();
        birdAnimator = GetComponent<Animator> ();
    }

    // Update is called once per frame
    void Update () {
        //Set the bird destnation
        birdAgent.SetDestination (target.position);

        //Measure the magnitude of the nav mesh agents velocity
        float speed = birdAgent.velocity.magnitude;

        //Pass the velocity to the animator component
        birdAnimator.SetFloat ("Speed", speed);
    }
}

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Jonathan,

The code seems correct, the only thing I can think of is did you add the Player object to the "Target" in the Bird Movement Script (Done in Unity)?

Select the Bird component from the hierarchy and in the inspector (should see the "Bird Movement Script" at the bottom with a "Target" The box should say "Player (Transform)" If it doesn't, either click the little circle and add "Player" or just drag the Player object from the hierarchy to the box.

If it's already there, then I apologize... I have no other suggestions.

:) :dizzy:

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Jason,

Unfortunately I have already taken that step and after restarting the bird behaves the same way.

But thanks for trying. :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

Hi Jason,

You may be interested to know that I figured it out.

Apparently the only way to make it would is to add the player object to the Transform field via the Hierarchy but not from the Prefab in the project manager.

I'm not sure what the difference is between the 2 but the hierarchy seems like the place to do that. :-)