Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 brewster2110 · May 30, 2018 at 03:45 AM · playerscript.enemynavmeshnavmeshagent

Tank not moving towards player

HI, I'm pretty new to Unity and have just started trying to use NavMeshAgent. i'm trying to make my enemy (A tank) move towards the player, but at the moment it kinda just sits then until it is hit and slides away. I think that means that my NavMesh is alright but my script is off? any help would be great thanks :)

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;

namespace BiddiscombeOlsson { public class SoldierControl : MonoBehaviour { // PUBLIC VARIABLES public bool playerInRange; public float thrust = 200f; public float dampening = 2.5f;

     void Update()
     {
         // If the player is in range ..
         if (playerInRange == true)
         {
             // .. find the player's position
             Transform targetLocation = GameObject.FindWithTag("Player").transform;
        
             // Set the reference to the soldier's navMeshAgent component
             UnityEngine.AI.NavMeshAgent navAgent = gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>();

             // Set the destination for the navAgent
             navAgent.SetDestination(targetLocation.position);

             // This makes the turret look at the player's X and Z but ignores the Y
             Vector3 playerPosition = new Vector3(targetLocation.position.x, transform.position.y, targetLocation.position.z);
             Vector3 aim = playerPosition - transform.position;
             Quaternion rotate = Quaternion.LookRotation(aim);

             // Lerp rotation and include the dampening to ease motion
             transform.rotation = Quaternion.Lerp(transform.rotation, rotate, Time.deltaTime * dampening);
         }
     }
 }

}

Thanks, can post other information if needed

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SohailBukhari · May 30, 2018 at 04:52 AM

Your code is fine. i think you declare the boolean playerInRange is not true in the inspector, thats why your NavmeshAgent is not moving.

Make Sure playerInRange is checked in the inspector. Inspector View

other thing is that you don't need the true check in the condition if (playerInRange == true), its true you can just write.

 if (playerInRange)


EDIT 1 : If that not move than another Update is blocking the Navmesh agent to move. Anywhere in the other script your playerInRange is false thats why Navmesh sits.

Note: Navmesh work only on Baked Areas which are marked as static.


navmesh.jpg (13.3 kB)
Comment
Add comment · Show 3 · Share
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
avatar image brewster2110 · May 30, 2018 at 07:55 AM 0
Share

Hi, thank you, my scene is set up so that when the player gets close enough to the tank it becomes "in range", just to make sure I tried checking it. This had no effect though. The tank still just sits in the ground looking at the player.

$$anonymous$$y Nav $$anonymous$$esh Agent: alt text

I have also made sure to bake it.

Thanks

capture.png (20.3 kB)
avatar image SohailBukhari brewster2110 · May 30, 2018 at 08:03 AM 0
Share

Where you setup player radius? the script you shared not defined anywhere the radius, the tank will move from anywhere without any radius calculation towards the player. playerInRange is true always so your tank will always move. If that not move than another Update is blocking the Navmesh agent to move. Anywhere in the other script your playerInRange is false thats why Navmesh sits.

avatar image brewster2110 SohailBukhari · May 30, 2018 at 10:38 AM 0
Share

I don't really understand Radius, but my "Agent Radius" in the bake section is set to 2? Didn't change anything when I altered this though. I've looked everywhere and made sure that playerInRange is set to true everywhere - no results. I cant find the problem anywhere, this is really bad :(. Thanks for the advice though

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

106 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

Related Questions

Nav Mesh Agent not smooth 1 Answer

NavMesh problem it does not respond corectly 0 Answers

Enemy follow Camera 0 Answers

NavMeshAgent Collision 1 Answer

No player is colliding with enemy even with capsule collider !!! Please me help me. 0 Answers


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