- Home /
WorldToScreenPoint is giving me a wrong X position?
I want to have a Unity GUI button float over an instantiated Game Object.
I have parented a Canvas and it's button to the prefab and have attached the follow script to the button (and filled the correct variables), but it positions the button entirely in the wrong position. What have I done wrong? Is this code correct? I have tried using both WorldToViewportPoint and WorldToScreenPoint, but I am given a wrong X position.
My canvas covers the whole screen and my button sits in the middle by default.
var characterObject : GameObject;
var button : GameObject;
var camera : Camera;
function Start()
{
ObjectToScreenPosition();
}
function ObjectToScreenPosition()
{
var buttonPosition : Vector3 = camera.GetComponent(Camera).WorldToScreenPoint(CharacterObject.transform.position);
button.GetComponent(RectTransform).anchoredPosition = buttonPosition;
}
Answer by juniperspark · May 18, 2016 at 06:50 PM
The solution to this was that my canvas of the button I wanted to move was not set to Screen Space - Overlay.
With Screen Space - Camera mode, i believe, the issue is the camera size. I'm not sure how to pick it properly, so i'll just stick to Screen Space - Overlay, it works for me as well.
Answer by tanoshimi · May 18, 2016 at 05:34 PM
On line 13, try the following instead:
button.GetComponent(Transform).position = buttonPosition;
It still doesn't work. The position I should be getting is something like: -481, 210, 0. but the position it is now spitting out is: 0.2, 0.4, 6.2.
I have tried changing anchor points on the button, but that doesn't seem to change anything. The Canvas is set at 0,0,0 with a 0.69 scale factor. The button is set at 0,0,0, with a 1 scale factor.
$$anonymous$$y camera is a Perspective camera with 60 FOV and a rotation of 40, 330, 350.
Would any of this be a problem?
Ah hang on, I read your post more carefully and I can spot some problems. "I have parented a Canvas and it's button to the prefab". Don't do that - you're setting the position via this script, so you don't want the canvas to inherit the position of any parent object. Place your canvas directly at the root of your scene hierarchy and try again. Also, be sure that your canvas is set to screenspace. There's also something weird going on with your z values. Here's the exact code that works for me when attached to a screenspace canvas to anchor it to any position in world space:
// Update is called once per frame
void Update () {
Vector2 sp = Camera.main.WorldToScreenPoint(anchor.position);
thisTransform.position = sp;
}
Hi again,
Sorry I read your post a little late. You are correct, the screenspace on the canvas was wrong and that was causing the errors. I had it set to screenspace camera ins$$anonymous$$d of overlay. Setting it to overlay seemed to solve the problem.
Answer by Jessespike · May 18, 2016 at 06:11 PM
hmm, I'm not really happy with this solution, but it works I guess. Surely there's a more elegant way to approach this. Well, at best it might give you some ideas how to resolve the issue. The snippet you posted contained script errors for me, so I renamed things a bit.
var m_characterObject : Transform;
var m_button : GameObject;
var m_camera : Camera;
function Update()
{
ObjectToScreenPosition();
}
function ObjectToScreenPosition()
{
var buttonPosition : Vector3 = m_camera.WorldToScreenPoint(m_characterObject.position);
m_button.GetComponent(Transform).localPosition = Vector3(
buttonPosition.x - (m_button.transform.parent.GetComponent(Canvas).pixelRect.width / 2),
buttonPosition.y - (m_button.transform.parent.GetComponent(Canvas).pixelRect.height / 2),
0);
}
Thanks for your answers and your code. @tanoshimi @Jessespike
I came across a couple of problems, one of which was randomly fixed and I can't tell what caused it: for some reason another script was interfering with it, which had nothing to do with that button. but I reverted to an old scene before I was mucking about and it sorted that one.
The main problem I think was that my canvas was set to Screen Space - Camera and changing this to Screen Space - Overlay fixed this.
Answer by Eyevi · Aug 29, 2018 at 01:48 PM
My problem was the same, but I found a very easy solution (even with "scale with screen size")
Code:
Vector3 viewPos = Camera.main.WorldToViewportPoint(anchorPoint.position);
GetComponent<RectTransform>().anchorMin = new Vector2(viewPos.x , viewPos.y );
GetComponent<RectTransform>().anchorMax = new Vector2(viewPos.x , viewPos.y );