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 locrizak · Jan 03, 2014 at 06:55 PM · follow playercamera.screentoworld4.3.3

WorldToScreenPoint conversion inconsistent

I've gotten the camera to follow my player pretty well. When the players position is past the half way height of the screen, the camera locks to the player. Otherwise the camera is locked to the player or at least that what is suppose to happen. As long as the character is ascending everything works great, when the player starts to fall everything gets a little glitchy.

Here is the code I am using to follow the player:

 void FixedUpdate (){
     float x = _xStart;
     float y = _yStart;
     Debug.Log (player.position.y);
     Vector3 pp = camera.WorldToScreenPoint(player.position);
     Debug.Log(pp.y);
     Debug.Log (pp.y > Screen.height/2);
     switch(WorldManager.GetGameState()){
         case GameState.LAUNCHED:
             //check if player is past mid screen before following
             x = pp.x > (Screen.width/2) ? player.position.x : _xStart;
             y = pp.y > (Screen.height/2) ? player.position.y : _yStart;
             break;
     }
     
     // Set the camera's position to the target position with the same z component.
     transform.position = new Vector3(x,y,transform.position.z);
 }

The state and all that is working fine. What I've noticed is pp.y is jumping up and down on the players decent. The logs display the following (obviously simplified):

 11.63518
 192.9192
 False
 11.48117
 633.3361
 True
 11.3255
 192.7765
 False
 11.1682
 619.8181
 True
 
 etc...

To me this points to there being a problem with camera.WorldToScreenPoint no converting the value properly? (which doesn't make sense to me) Can anyone see anything that I'm messing up. If you need any more info please ask.

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 Talimar · Nov 28, 2015 at 09:31 AM 0
Share

I am having this exact problem now in 5.2.2 and now 5.2.3. It seems a physical impossibility. I have verified the input is the same each frame, and the same camera is being used, no other variables are changing, but my x value is jumping between two distinct values every frame, I can position my viewer to see both places it jumps to at the same time, and for each position, the alternating results are consistently the same (logging the results with collapse on shows 1 input and 2 outputs, each with half as many counts as the input).

It's sad that you haven't got an answer or even a comment 2 years, I was going so well with my game but this is a major roadblock.

Ok, after announcing my issues to the world, I solve the problem (my life story). I actually had 2 pieces of code fighting over the rotation of my object, and it was only a happy coincidence that at the start of the game they were aligned.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Foo-Byte · Nov 28, 2015 at 11:26 PM

I have seen WorldToScreenPoint jitter, but not as bad as the OP's output. I would guess that in this case it's not actually a problem with WorldToScreenPoint, but with the camera snapping back and forth between centering on the character and some other location. In the code above, I don't see where _xStart and _yStart are adjusted, and it seems like the camera would bounce between that position and the player's position.

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

ScriptableObject asset loses reference after restarting Unity 1 Answer

Instantiating objects to trail player 2 Answers

gameObject empty follow player but don't get out local position 0 Answers

enemy flying through air when targeting player 1 Answer

Camera rotating around player, but passes trough objects 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