• 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 DRAF · Aug 08, 2015 at 01:25 PM · objectnavmeshobstacleagentsame

enable/disable navmesh agent and obstacle same object (player)

hi mates,

I'm about to make my player move to clicked point and to prevent other game object to push my player around (vice versa) my code is something like this =

if raycast to ground -enable agent -disable obstacle -move to hit.point

if player arrive to destination || player no movement -enable obstacle -disable agent

can someone help me to code this??

Comment
Add comment · Show 3
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 DRAF · Aug 08, 2015 at 01:25 PM 1
Share

its c sharp. my code is as follow:

 void Start () 
 {
     agent = GetComponent<Nav$$anonymous$$eshAgent> ();
     obstacle = GetComponent<Nav$$anonymous$$eshObstacle> ();
 }

 void Update () 
 {
     if (Input.Get$$anonymous$$ouseButtonUp (0)) 
     {
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast (ray, out hit))
         {
             if (hit.collider.tag == "Ground")
             {
                 agent.enabled = true;
                 obstacle.enabled = false;
                 agent.destination = hit.point;
             }
         }
     }
     if (agent.velocity == Vector3.zero) 
     {
         agent.enabled = false;
         obstacle.enabled = true;
     }
 }

when move, i can get my player agent enabled & obstacle disabled. but when it stop, it doesn't turn into agent disabled obstacle enabled

avatar image allenallenallen · Aug 08, 2015 at 01:26 PM 0
Share

I see. In your start function:

  void Start () 
  {
      agent = GetComponent<Nav$$anonymous$$eshAgent>();
      obstacle= GetComponent<Nav$$anonymous$$eshObstacle>(); // You wrote "agent" ins$$anonymous$$d of "obstacle"
  }


EDIT: Nvmd, you fixed it.

avatar image DRAF · Aug 08, 2015 at 01:30 PM 0
Share

Just now typo during copy paste.. i have repost the corrected one.. but still not working

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Graphics Problem and NavMesh Obstacles 0 Answers

Navmesh stuck on obstacle 0 Answers

Nav Obstacle bug? 1 Answer

Draw path along Navmesh agent path 3 Answers

Toggling NavMeshAgent and NavMeshObstacle 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges