Currently following a older tutorial and I can not for the life of me figure this one issue out

I am currently following a tutorial and I have a section of code that is not working. I checked the tutorial and compared it to my code and I can not find what is wrong. What is supposed to happen is that when I select an object my character moves towards it, but if I hit mouse button 1 it should stop. For some reason the focus is removed but my character continues to move. All help is appreciated.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.AI;
[RequireComponent(typeof(NavMeshAgent))]
public class PlayerMotor : MonoBehaviour
{
Transform target;
NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
agent = GetComponent();
}
void Update()
{
if (target == null)
{

}
if (target != null)
{
agent.SetDestination(target.position);
FaceTarget();
}
}
public void FollowTarget(Interactable newTarget)
{
agent.stoppingDistance = newTarget.radius * .3f;
agent.updateRotation = false;
target = newTarget.interactionTransform;
}
public void StopFollowingTarget()
{
agent.stoppingDistance = 0f;
agent.updateRotation = true;
target = null;
}
void FaceTarget()
{
Vector3 direction = (target.position - transform.position).normalized;
Quaternion lookrotation = Quaternion.LookRotation(new Vector3(direction.x, 0f, direction.z));
transform.rotation = Quaternion.Slerp(transform.rotation, lookrotation, Time.deltaTime * 5f);
}
}

Error is happening in the StopFollowingTarget are.

A few things:

  • First, please use code tags to share your code in the forums. Otherwise as you can see it’s very difficult to read your code. Look for this button:

6341787--704583--upload_2020-9-23_15-7-28.png

  • If you are getting an error, please copy and paste the entire error message here.
1 Like

There are no errors. Also code has been uploaded

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class PlayerMotor : MonoBehaviour
{
    Transform target;
    NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        if (target != null)
        {
            agent.SetDestination(target.position);
            FaceTarget();
        }
    }

    public void FollowTarget(Interactable newTarget)
    {
        agent.stoppingDistance = newTarget.radius * .3f;
        agent.updateRotation = false;

        target = newTarget.interactionTransform;
    }

    public void StopFollowingTarget()
    {
        agent.stoppingDistance = 0f;
        agent.updateRotation = true;

        target = null;
    }
    void FaceTarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookrotation = Quaternion.LookRotation(new Vector3(direction.x, 0f, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookrotation, Time.deltaTime * 5f);
    }
}

You are not updating the NavMeshAgent’s destination when StopFollowingTarget is called so it will continue to navigate.

Ok so how do I fix this?

This should work:

    public void StopFollowingTarget()
    {
        agent.stoppingDistance = 0f;
        agent.updateRotation = true;        
        agent.SetDestination(transform.position);
        target = null;
    }

So it kinda worked. My guy was no longer focused on the point but then the controls got all weird and his movement wasn’t right

Maybe play with this property as well? Unity - Scripting API: AI.NavMeshAgent.isStopped

1 Like

That did it!!! Thank you for your help!