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 Tyler 11 · Mar 26, 2011 at 05:30 PM · cameraplayerview

Bounds of players on screen

Well hello there, I am trying to get the furthest left and furthest right player on the screen (this is a 2D platformer so math is simple) but I'm having an issue with my code. This is supposed to find the furthest indexed player to the right and the left on the screen and it works for the furthest RIGHT as far as I can tell but it doesn't always detect the LEFT most character, it seems to skip an index. I know theres a logical flaw here I'm just failing to find it at the moment.

if(posPrev == 0) posPrev = players[0].transform.position.x; for(int i = 0; i < numPlayers; i++) { posCur = players[i].transform.position.x;

             if(posCur &gt; posPrev)
                 largeIndex = i;
             if(posCur &lt;= posPrev)
                 smallIndex = i;
             posPrev = posCur;

         }

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

2 Replies

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

Answer by Owen-Reynolds · Mar 26, 2011 at 06:42 PM

posPrev is always the x-coord of the player just before you. The line if(posCur > posPrev) says you are the new winner if you beat the last guy, not the best so far. If you run it on enough inputs, you'll see it can be wrong on L or R (ex: 3,9,6,7) says 6 is left (less than 9) and 7 is right (more than 6.)

The standard "find best/worst array search" closest to what you have is this:

// Assume item 0 is the furthest left and right, for now:
float largeX = players[0].transform.position.x; // save index and pos of largest
int largeIndex = 0;
float smallX = players[0].transform.position.x; // same for smallest
int smallIndex = 0;
for(int i = 0; i < numPlayers; i++)
{
   int posCur = players[i].transform.position.x;
   if(posCur > largeX) {
     largeIndex = i; largeX = posCur; // update index and pos of new largest
   }
   else if(posCur < smallX) {
     smallIndex = i; smallX = posCur; // update index/pos of new smallest
   }
}
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 Tyler 11 · Mar 26, 2011 at 06:49 PM 0
Share

Why yes, that works perfectly. Thank you very much!

avatar image
0

Answer by Bampf · Mar 26, 2011 at 06:22 PM

Might be an initialization problem. You are initializing posPrev, but not largeIndex and smallIndex, so (depending on the rest of your code) it's possible that old values will leak through.

One way to do this would be to set largeIndex and smallIndex initialized to 0 when posCur is initialized to players[0].transform.position.x. (You could also start your loop at i = 1 in that case.)

I'm also confused by the first line, the IF condition. Why the test for posPrev == 0?

Comment
Add comment · Show 2 · 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 Tyler 11 · Mar 26, 2011 at 06:48 PM 0
Share

I had largeIndex and smallIndex initialized in Start() to 0, sorry I didn't post that.

avatar image Bampf · Mar 27, 2011 at 02:18 PM 0
Share

$$anonymous$$y point was you need to reset them every time you run your loop, not just in Start(). The solution posted by Owen does exactly that (as well as the other fix.)

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

No one has followed this question yet.

Related Questions

Make object transparent when between camera and player 11 Answers

Clarification with Texture2D.ReadPixels. 1 Answer

Camera watching inbetween the player and cursor 1 Answer

What is the typical setup for switching between third and first person, in terms of model culling? 0 Answers

Multiplayer| Attaching camera to player (if i am the owner 1 Answer


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