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 tawalke · Jun 17, 2015 at 05:52 PM · scripting problemgameobjectscript.vector3

Calculating Scrolling GameObject x position scrolling pass another GameObject x postion (2D Game)

I would appreciate any help that can be provided as the reason this code isn't working doesn't make sense to me.

I have a collection/array of obstacles that are moving across the screen (x position) from right to left. I have a player/ship that is located on the left side of the screen.

I would like to kick off a method once one of the obstacles have passed the ship. When I run my code the function kicks off before the object even makes it to the middle of the screen. I don't understand why this is happening.

My code is as follows:

 void Update () 
 {
     CheckObstaclePosition();
 }
 
 
 void CheckObstaclePosition() 
 {
   Obstacles[] obstaclesIns;
   Obstacles currObstacle; 
 
   obstaclesIns = FindObjectsOfType();
 
   if (obstaclesIns.Length > 0) 
      {
         currObstacle = obstaclesIns[0];
         currObstacleVector = currObstacle.transform.position;
         currShipVector = this.gameObject.transform.position;
         
         currObstaclePos = currObstacleVector.x;
         currShipPos = currShipVector.x - 0.2f;
            
         if (currObstaclePos < currShipPos)
            {
                 Debug.Log("Obstacle passed Ship");
                 AnimateExplosion();  
                 currObstacle.DestroyObstacle();
             }
 
      }  

  

Can someone explain why this behavior is happening and why the x position of the obstacle is showing that it is passing the ship before it even gets close to the middle of the screen let alone actually getting pass the ship.

Comment
Add comment · Show 1
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 tawalke · Jun 18, 2015 at 03:12 AM 0
Share

Help Anyone?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by duck · Jun 18, 2015 at 10:00 AM

First, you're don't have any "type" specified in your FindObjectsOfType command. I haven't seen this before, but I'm guessing it's just finding all objects in your scene perhaps? And then testing one that you didn't intend to test against.

So you need a better way of identifying your list of "obstacles". You could give them all a tag, and use GameObject.FindGameObjectsWithTag, or you could attach them all to a parent object, and enumerate through that object's childre,

Second, in your code, you're not checking each obstacle in your list of obstacles - only the 1st (item 0) because of your code which says: obstaclesIns[0]. You haven't set up a loop to check through each obstacle in the list.

You need to look up "for loops" or "foreach loops"and how to use them.

Comment
Add comment · Show 1 · 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 tawalke · Jun 18, 2015 at 01:52 PM 0
Share

I only want the first one each time to generate an explosion, so I don't want to loop through all the obstacle. These are spawned dynamical and destroyed in another script.

I am successfully getting the object I want, so my problem is not with the Find call. The issue is that when I add the animation call to check the different in location it only goes half way across the screen and then call the animation. When I take it out the obstacle(s) go completely across the screen. That is the question. Not a newbie coder, a simple foreach loop is not my issue. I just want to understand this behavior and why the Boolean check for it surpassing the threshold is correct based upon the logs but on screen it is not displaying the animation accurately.

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

GameObject is already being activated or desactivated 2 Answers

How to detect if two objects become a square ? 1 Answer

Please help my head is burning from this problem : i have multiple gameobject , same script 1 Answer

How to double spirte/gameobject/prefab and control the result on those items? 0 Answers

How do I play gameobjects in my scene from one script / Script is not working 0 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