Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'Scripting' started by ajmracer032706, Sep 23, 2020.

  1. ajmracer032706

    ajmracer032706

    Joined:
    Sep 15, 2020
    Posts:
    24
    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<NavMeshAgent>();
    }
    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.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    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:
    upload_2020-9-23_15-7-28.png

    • If you are getting an error, please copy and paste the entire error message here.
     
    Vryken likes this.
  3. ajmracer032706

    ajmracer032706

    Joined:
    Sep 15, 2020
    Posts:
    24
    There are no errors. Also code has been uploaded
     
  4. ajmracer032706

    ajmracer032706

    Joined:
    Sep 15, 2020
    Posts:
    24
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Collections.Specialized;
    5. using System.Security.Cryptography;
    6. using UnityEngine;
    7. using UnityEngine.AI;
    8.  
    9. [RequireComponent(typeof(NavMeshAgent))]
    10. public class PlayerMotor : MonoBehaviour
    11. {
    12.     Transform target;
    13.     NavMeshAgent agent;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         agent = GetComponent<NavMeshAgent>();
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (target != null)
    24.         {
    25.             agent.SetDestination(target.position);
    26.             FaceTarget();
    27.         }
    28.     }
    29.  
    30.     public void FollowTarget(Interactable newTarget)
    31.     {
    32.         agent.stoppingDistance = newTarget.radius * .3f;
    33.         agent.updateRotation = false;
    34.  
    35.         target = newTarget.interactionTransform;
    36.     }
    37.  
    38.     public void StopFollowingTarget()
    39.     {
    40.         agent.stoppingDistance = 0f;
    41.         agent.updateRotation = true;
    42.  
    43.         target = null;
    44.     }
    45.     void FaceTarget()
    46.     {
    47.         Vector3 direction = (target.position - transform.position).normalized;
    48.         Quaternion lookrotation = Quaternion.LookRotation(new Vector3(direction.x, 0f, direction.z));
    49.         transform.rotation = Quaternion.Slerp(transform.rotation, lookrotation, Time.deltaTime * 5f);
    50.     }
    51. }
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    You are not updating the NavMeshAgent's destination when StopFollowingTarget is called so it will continue to navigate.
     
  6. ajmracer032706

    ajmracer032706

    Joined:
    Sep 15, 2020
    Posts:
    24
    Ok so how do I fix this?
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
    This should work:
    Code (CSharp):
    1.     public void StopFollowingTarget()
    2.     {
    3.         agent.stoppingDistance = 0f;
    4.         agent.updateRotation = true;        
    5.         agent.SetDestination(transform.position);
    6.         target = null;
    7.     }
     
  8. ajmracer032706

    ajmracer032706

    Joined:
    Sep 15, 2020
    Posts:
    24
    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
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,697
  10. ajmracer032706

    ajmracer032706

    Joined:
    Sep 15, 2020
    Posts:
    24