Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by tamas3000 · Nov 29, 2021 at 02:30 PM · movementnavmeshagentdistancestopping

Stopping navmesh agent next to the enemy

Hello everybody,

I am a Unity student and working on an RTS game.

I have a guy who has 3 weapons: knife, gun and UMP45. He can stab the enemy from a distance if next to the enemy and can gun down them when inside a certain distance of them. Range of the gun and ump obviously. I have issues with preparing the character for the stabbing animation. I would like to stop the main character next to the enemy soldier in a distance like 1 meter when I clicked on the soldier. I cannot stop the guy he keeps going into the position of the soldier and overlaps the enemy instead of stopping next to him by a distance. I have tried stopping distance, by some issues it is not working in my case. Remaning distance gives me weird amounts when I measure it on the scene.

Here is what I have so far:


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.AI;

using UnityEngine.EventSystems;

public class AiMoveToClick : MonoBehaviour

{

 // Making the main character to be able to move where the player clicks //

 public GameObject player;

 // Reference for the navmesh agent component
 NavMeshAgent playerNavmesh;

 Animator playerAnim;

 public bool canStab;

 WeaponSelection weaponSelectionScript;

 Transform target;

 public bool enteredTrigger;

 Vector3 offset =  new Vector3(4.0f, 0, 4.0f);
 // Start is called before the first frame update
 void Start()
 {
     playerNavmesh = GetComponent<NavMeshAgent>();

     playerAnim = GetComponent<Animator>();

     weaponSelectionScript = FindObjectOfType<WeaponSelection>();

     playerAnim.SetBool("IsWalking", false);

     canStab = false;
 }

 // Update is called once per frame
 void Update()
 {

     
     // if we click with the left mouse button
     RayCastCheckAndMove();

     if(Input.GetKeyDown(KeyCode.G))
     {
         playerAnim.SetTrigger("IsStabbing");
     }
 }

 public void RayCastCheckAndMove()
 {
     if (Input.GetMouseButton(1))
     {
         target = null;
     }

     if (Input.GetMouseButton(0))
     {
         // if the UI is selected do not do anything
         if(EventSystem.current.IsPointerOverGameObject())
         {

             return;
         }

         // temp storage for Raycast information , new Raycast variable
         RaycastHit hit = new RaycastHit();


         // IF Raycast inside Physics class
         // If the raycast coming from the camera hits the position (where the mouse click happens)
         // store the information into hit variable and take a distance for Raycast as 100
         if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
         {
             if (hit.collider.CompareTag("Plane"))
             {
                 playerNavmesh.stoppingDistance = 0;
                 // move the navmesh agent to the mouse click's position
                 playerNavmesh.destination = hit.point;

                 //Debug.Log("You clicked on: " + hit.collider.tag);
             }

             if (hit.collider.CompareTag("Soldier1") && weaponSelectionScript.knifeIsSelected)
             {
                

                 //canStab = true;

                 target = hit.transform;

                 playerNavmesh.destination = target.position + offset;

                 if (playerNavmesh.remainingDistance <= 0)
                 {
                     playerNavmesh.speed = 0;
                     //playerAnim.SetBool("IsWalking", false); 
                     playerAnim.SetTrigger("IsStabbing");
                 }
             }
         }

         // if remaining distance to destination is more than 0 meaning we are on our way
         if (playerNavmesh.remainingDistance > 0)
         {
             // switch on the walking animation sequence with a boolean
             playerAnim.SetBool("IsWalking", true);
         }

     }

     // if the remaining distance is 0, meaning we have arrived to destination
     if (playerNavmesh.remainingDistance <= 0)
     {
         // switch off the walking animation sequence with a boolean
         playerAnim.SetBool("IsWalking", false); 
     }
 }

}

Can I get help on this please?

Thank you very much

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

187 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NavMeshAgent's stopping distance doesn't work 0 Answers

Rigidbody slows down before touching its target 1 Answer

focusing on a unit during unit detection in an RTS 1 Answer

Best practice to make NavMeshAgent chase a moving target? 1 Answer

Move object towards an angle the distance of two points 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges