Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Rin · Feb 04, 2015 at 08:43 AM · movetounit

Make one unit move towards another.

Edit: Okay tossed out the previous script after digging up some tutorials yet this still isn't working any help?

I am working on some basic scripts so that I can use them as the basis for a few RTS games I have in mind. I wrote out this script and applied it but for some reason the unit it is attached to is not moving towards the enemy. Anyone have any ideas what I am doing wrong? Oh I should also note that when I put it in the editor and tell it to just go after the object it will start flying off like crazy in some random direction.

   private var human : Transform;
   private var nav : NavMeshAgent;
   private var LOS = 10;

   function Start () {
   human = GameObject.FindGameObjectsWithTag ("Human").transform;
   nav = GetComponent (NavMeshAgent);
   }

   function Update () {
    nav.SetDestination(human.position);
   }

Also I have another script to create the tags as vars it goes


       public static var human : String = "Human";

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 karljj1 · Feb 03, 2015 at 09:22 PM 0
Share

If you have a Nav$$anonymous$$esh agent then that could be overriding your position. Try letting the Nav$$anonymous$$eshAgent move the character ins$$anonymous$$d of using $$anonymous$$oveTowards or disable the Nav$$anonymous$$eshAgent.

avatar image Rin · Feb 03, 2015 at 10:09 PM 0
Share

Didn't know about that will have to experiment.

avatar image Rin · Feb 04, 2015 at 03:44 AM 0
Share

Tore it apart and redid it but still nothing is happening.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Feb 04, 2015 at 09:10 AM

Not well up on JS but your code looks absolutely fine. I'll take it as read that you've baked the navmesh into the scene already so let's get some idea of what's going on. What is the "Human" character? It's just most people call their enemies enemy or something bad.

If this is the enemy then that's fine if it's the character you want to move then that's where the error is. You'd be asking the character to move to it's own location, it's already there so it'll just stay where it is.

Test this by putting an empty GameObject on the scene and call it something unique like MoveToHere then try that instead of heading to your Human character. Or even just nick this direct from the Scripting API page:

     private var agent: NavMeshAgent;

     function Start () {
         agent = GetComponent.<NavMeshAgent>();
     }

     function Update () {
         var hit: RaycastHit;
         // When the mouse is clicked...    
         if (Input.GetMouseButtonDown(0)) {
             // If the click was on an object then set the agent's
             // destination to the point where the click occurred.
             var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             if (Physics.Raycast(ray, hit)) {
                 agent.SetDestination(hit.point);
             }
         }
     }

Unity staff wrote that code so if it doesn't work then you know there's some major component missing.

Comment
Add comment · Show 10 · 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 Rin · Feb 04, 2015 at 04:17 PM 0
Share

I am working on a RTS game and one of the factions is Human so I figured having a tag for them would be good. Lemme test your code.

avatar image Rin · Feb 04, 2015 at 04:22 PM 0
Share

Your script works so all the things seem to be in thee so I am not sure what is wrong with $$anonymous$$e?

avatar image Rin · Feb 05, 2015 at 02:22 AM 0
Share

Bit of an update I am noticing the error pop up "NullReferenceException: Object reference not set to an instance of an object Basic Enemy.Update () (at Assets/Basic Enemy.js:11) " Does that help?

avatar image Mmmpies · Feb 05, 2015 at 11:24 AM 0
Share

Sorry @Rin night fell. Yes null reference is important and that line is the nav line, chances are it's not picked up the Nav$$anonymous$$eshAgent. $$anonymous$$y knowledge of JS is poor but the documentation suggest your line is O$$anonymous$$, having said that if the example code worked try setting the line as they do in that code.

 nav = GetComponent.<Nav$$anonymous$$eshAgent>();

Let's also try some Debug.Log lines so we know where we are.

 function Update () {
     if(nav != null)
     {
         nav.SetDestination(human.position);
         Debug.Log("my pos = " + transfrom.position);
         Debug.Log("human pos = " + human.positon);
     }

avatar image Rin · Feb 05, 2015 at 07:17 PM 0
Share

I put in the debug code but I am getting an error on the "my pos" line saying "unknown identifier transform".

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Object rotation to unit vector 1 Answer

Component based architecture Unit, AI, Health, Attack 0 Answers

iTween MoveTo/MoveUpdate 1 Answer

how can i throw the object? need all step to create and throw. 1 Answer

Help making script!! 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