• 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 /
  • Help Room /
avatar image
0
Question by SuperRockmen · Aug 30, 2015 at 08:00 AM · gameobjectpathfindingdisappearpathfollowingtower defense

Sprite disappearing when playing and fixed path finding for tower defense games

Basically I am trying to make my gameObjects follow a fixed path in my tower defense game:

My idea is that I can create an array of invisible waypoint that will be attached to a single gameobject which the units can follow:

This is my hierachy view: link text

These are my inspector views (3 images): link text

I have attached the "Pathing" script to the "Path 1" gameobject which includes all my waypoints:

 public class Pathing : MonoBehaviour {
     public Transform[] path;
 
 
     // Use this for initialization
     void Start () {
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public Transform[] getPath(){
         return path;
 }
 }

I have attached the "PathFollower" script to my unit which will follow the waypoint:

 public class Pathfollower : MonoBehaviour {
  
     public int currentPoint=0;
     public Transform[] path;
     public Pathing other;
     public float speed=50.0f;
     // Use this for initialization
 
 
     void Start () {
         path=other.getPath();
     }
     
     // Update is called once per frame
     void Update () {
         Debug.Log(currentPoint);
     float dist =Vector3.Distance (path[currentPoint].position, transform.position);
     transform.position=Vector3.MoveTowards(transform.position,path[currentPoint].position,speed*Time.deltaTime);
 
         if(dist==0f){
             currentPoint++;
         }
     
         if(currentPoint>=path.Length){
             Debug.Log("reset");
             currentPoint=0;
         }
 
     }
 }

However, when I play the scene, this happens: link text

Questions:

1) Why did my unit disappear halfway?

2) Are there any more efficient ways to implement fixed path finding?

Thanks for any help rendered.

Comment
Add comment · Show 2
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 SuperRockmen · Sep 01, 2015 at 01:44 PM 0
Share

Anyone has an idea on what could cause this?

avatar image Baste · Sep 01, 2015 at 02:01 PM 0
Share

The only thing I can think of that might be causing this is if your path points have a different z-value from the sprite, they might be behind the camera (or the camera's clipping plane), so the sprite stops being shown when it gets close enough point. If you're using sprites on a canvas, or some other 2D setup, using Vector2 for positioning will make sure that it only moves on the x/y plane.

For 2), effeciency isn't a problem here. I'd move the dist = Vector3.Distance... to after the transform.position = ... call, as you want to change the direction you're moving immediately after getting to your waypoint.

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

28 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

Related Questions

Survival Shooter Tutorial - Applying Layer causes model to disappear (SOLVED) 1 Answer

How to I make a gameobject follow a list of nodes and to link an instantiated gameobject to a script. 0 Answers

Unity NavMesh path-finding with my own moment script. 1 Answer

Hi, I'm working on a city traffic project My problem is that cars always   At each point of the path they come to the starting point of the track when starting the game While I want to move where they are placed along the designated path this is my code 0 Answers

pathfinding Error or follow player 0 Answers

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