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 adriandevera · Jun 21, 2015 at 11:16 AM · moveisometricnodesnearest

Isometric Move to nearest Node

SO this node system currently is suitable for a Tower Defense game where nodes are predefined in an Array and move to the next in order. How would I implement a feature to move to the nearest node to the character within a range x number of tiles?

 using UnityEngine;
 using System.Collections;

 [RequireComponent(typeof(IsoObject))]
 [ExecuteInEditMode]
 public class Node : MonoBehaviour {
 
     [SerializeField]
     private IsoObject obj;
 
     public Vector3 Position {
         get { return obj.Position; }
         private set { obj.Position = value; }
     }
 
     void Start() {
         obj = GetComponent<IsoObject>();
 
     }
 
     void OnDrawGizmos() {
         Gizmos.color = Color.yellow;
         Gizmos.DrawSphere(Isometric.isoProjection(obj.Position), .1f);
     }
 }

Heres the controller

 using UnityEngine;
 using System.Collections;
 
 public class NodeIsoObjectController : AbstractIsoObjectController {
     //units/second
     public float speed = 2f;
     public int movement = 5;
     public bool loop = true;
     public Node[] nodes;
 
     public int nextNode = 0;
 
     void Start() {
         this.isoObj = GetComponent<IsoObject>();
         moveToNextNode();
     }
     public void moveToNextNode() {
 
         if (nodes.Length > 0 && nodes[nextNode] != null) {
             var newPos = nodes[nextNode].Position;
             float distance = (isoObj.Position - newPos).magnitude;
 
             moveTo(newPos, (x) => EasingFunctions.Linear(x, 0, 1, distance / speed), () => {
                 if (nextNode < nodes.Length - 1) {
                     nextNode++;
                     moveToNextNode();
                 } else {
                     if (loop) {
                         nextNode = 0;
                         moveToNextNode();
                     }
                 }
 
             }, 0, distance / speed);
         }
     }
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 codebeans · Jun 22, 2015 at 06:56 AM

Hi, since the original question referred to something like a pedestrian system such as in "Roller coaster tycoon", I will try to give you some input on that. This is not specific to my Asset but rather an overall explanation.

The reason is I have a sprite that will walk around and wander on its own and will walk from one tile to the nearest (depending on the distance cost of how far they are willing to go). Think of it like Rollercoaster tycoon where each walkway has a node and if there is a fork in the road the people will select randomly between the two and proceed to move to the nearest next tile.

A very simplified version of your required behaviour could be implemented like this:

Have a overall node system, something that stores all nodes or holds references to them. Use a singleton combined with a observer pattern to get all references of Node instances easily . (Just so that you can access all nodes without polling GetComponents or something with big overhead).

Now we assume you have a player in your scene which has a position (doesn't matter if we are talking about IsoObject.Position or transform.postion or..) as a Vector3. The Nodes do have a position as Vector3 as well. So when your player has to decide its next node to go to just loop over all nodes an calculate the distance via Vector3.Distance(playerPosition,nodePosition). Then find the minimum. You could chop the maximum distance by your

range x number of tiles

And just move to that node. This would probably lead to the problem that your player doesn't move at all since the minimum distance to the next node might be the one he is standing on right now, or even worse the one he came from. So he would go back and forth between the two closest nodes.

So make sure to exclude the current and last node from your calculations.

This would be a very simple pedestrian system. It's performance would be pretty bad and I wouldn't recommend such an implementation for a final product. But as a proof of concept or some rapid prototype this might be suitable.

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help getting my character to move 2 Answers

Prefab connected to empty gameobject with scripts, empty moves, prefab doesnt? 2 Answers

Make Object Move to/follow Another Object? (plus turn towards it) 3 Answers

First Person Controller will not move 6 Answers

Camera and Target help. 3 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