- Home /
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.
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.
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.
Your answer
Follow this Question
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