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 way3edgy · Feb 15, 2014 at 04:55 AM · movementtransformarraysobjectswaypoints

Movement on roads issue

So I'm trying to get a computer character to follow a road map. Basically it picks a random intersection to move towards, only re-adjusting if it wanders off the road by moving towards the closest road. I spent a few days trying to fix the original problem of it wandering off of the road randomly, but now I have another problem:

The character follows a loose pattern along the road, but doesn't save the closest road; instead it picks a random one far away. Also, the character still isn't moving towards the intersection that it says it's going after. I used a Debug.Log to print the point and it's setting it as just a vague (Object) instead of an actual point...here is the code I have so far:(One note, I excluded the variable initializations)

EDIT: Thanks to superluigi's advice, I started using navmesh...this is the new code in case anybody else needs it.

 #pragma strict
 
 var agent: NavMeshAgent;
 var target: GameObject;
 var moveSpeed : float = 6.0;
 var gravity : float = 10.0;//basic stuff
 var wayPointsAr: GameObject[] ;
 var MyIndex;
 var dist:float;
 
 function Awake()
 {
 agent=GetComponent(NavMeshAgent);
 wayPointsAr= GameObject.FindGameObjectsWithTag("waytag");//tags all waypoint objects
 MyIndex = Random.Range(0,(wayPointsAr.length - 1));
 target=wayPointsAr[MyIndex];
 Debug.Log(target.transform.position);
 }
 
 function Update() 
 {
 agent.SetDestination(target.transform.position);
 dist=Vector3.Distance(target.transform.position,transform.position);
 if (dist<50)
     {
     MyIndex = Random.Range(0,(wayPointsAr.length - 1));
     target=wayPointsAr[MyIndex];
     Debug.Log(target.transform.position);
     }
 }
 
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 superluigi · Feb 15, 2014 at 05:08 AM

Why don't you just use navmeshes. You can create the roads and your character will never wander off because it cant. Navmeshes also have built it AI so say for example your character chooses a random spot to go to but there are 3 different paths it can take, it'll automatically choose the shortest path available. There is a tutorial video here on unity's website that does an excellent job of teaching you about navmeshes.

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 superluigi · Feb 15, 2014 at 05:10 AM 0
Share

link text

avatar image way3edgy · Feb 15, 2014 at 05:23 AM 0
Share

Ah! I'll have to look into that. I was basically creating an object for each piece of the road...pretty primitive stuff, didn't even know there was such a thing. I'll look into it and update it once I try it out. Thanks luigi.

avatar image way3edgy · Feb 16, 2014 at 06:14 AM 0
Share

Good news, after a few hours of experimentation the navmesh works. Thanks again.

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

19 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

Related Questions

Npc Movement 0 Answers

How can i synchronize prefab with NetworkTranform? 1 Answer

Unity 2D Scripted Movement 2 Answers

Move multiple objects relative to another objects movement 1 Answer

How can I make a prefab object from a list to move?,How can I make an object from a list to move by itself? 2 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