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 Magnomous · May 02, 2014 at 08:36 AM · c#transformtrigger

Ignoring first move point

Hello, I just started doing tower defense game and as first I am trying to do enemy movement. But the thing is, that enemies after instantiate skip first point to move and goes straight to 2nd, then to 3rd as they should. But can't figure out why they skip that first point... Here is the script:

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class EnemyMovement : MonoBehaviour {

 public float movementSpeed;
 public List<Transform> movePoints;  
  
 private int _index;
 private Vector3 _moveDirection;

 void Start() {

     GameObject[] pointsToMove = GameObject.FindGameObjectsWithTag("MovePoint");
     foreach(GameObject ptm in pointsToMove) {

         movePoints.Add(ptm.transform);
     }

     _index = 0;
     _moveDirection = (movePoints[_index].position - transform.position).normalized;
 }

 void FixedUpdate() {

     transform.position += _moveDirection * movementSpeed * Time.deltaTime;
 } 

 void OnTriggerEnter(Collider other) {

     if(other.tag == "MovePoint") {

         _index++;
         
         if(_index == movePoints.Count-1) {
             Destroy(gameObject);
         }
         else {
             _moveDirection = (movePoints[_index].position - transform.position).normalized;
         }
     }
 }

}

And here is a picture:

alt text

picture.jpg (145.1 kB)
Comment
Add comment · Show 4
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 Jenesis · May 02, 2014 at 08:50 AM 1
Share

Can you confirm which objects are tagged with $$anonymous$$ovePoint - is it the three movement points and nothing else (i.e. not spawnPoint I can see along side the movement points in the editor). It's possible they're colliding with that when they spawn.

avatar image Magnomous · May 02, 2014 at 08:56 AM 0
Share

spawnPoint is an empty game object without any collider... At the same time only Position1,2,3 are tagged as $$anonymous$$ovePoint. Nothing else.

avatar image Jenesis · May 02, 2014 at 09:11 AM 1
Share

Is the collision code actually being hit when they spawn and ignore point 1? Stick a Debug.Log line in and see if it's being called at all. I suspect what's actually going on is that the points just aren't in the right order.

avatar image Magnomous · May 02, 2014 at 09:22 AM 0
Share

I just added more $$anonymous$$ovePoints and realized that you are right. $$anonymous$$ovePoints are indeed not added to the list in the right order. That was the problem. $$anonymous$$ake your comment as answer please. And thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jenesis · May 02, 2014 at 09:38 AM

GameObject.FindGameObjectsWithTag doesn't make any guarantees about the order it'll return its results in. So what's happening is that Point2 ends up first in the list and the enemies are moving straight to that point.

Ordering the points shouldn't be too hard - see answers to this question. I'd go with the 'find them all and then sort them' method rather than searching for them all individually by name, which will be a pain, especially if you add more.

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

21 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

Related Questions

Destory/Collect object to open door 1 Answer

Distribute terrain in zones 3 Answers

TriggerExit not working as intended/Transforming GameObject Problems 0 Answers

Multiple Cars not working 1 Answer

Door Between Scenes 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