- Home /
Raycast has offset when build is not in native aspect ratio
Raycast has offset when build is not in native aspect ratio (only in full screen mode) I have a script that adds black bars. However I am using mouseposition in raycast to find the position. Basically when the resolution has a different aspect ratio than 16:9 it creates 4:3 with black bars which somehow works for some resolution but when the scaling is too much it fails to work making me unable to click on gameobjects. Raycast script:
///////////////////////////////////
void CastRayOne() {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100)) {
Debug.DrawLine (ray.origin, hit.point);
Debug.Log ("Hit object: " + hit.collider.gameObject.name);
if(hit.collider.gameObject.tag == "Character"){
CharacterHelper.SetActive(false);
CustomizerText_Character.text = hit.collider.gameObject.name;
Instantiateposition = new Vector3(hit.collider.transform.position.x+2,hit.collider.transform.position.y,hit.collider.transform.position.z);
Instantiatedobject = (GameObject)Instantiate (Customizer_Character,Instantiateposition,Customizer_Character.transform.rotation);
Instantiatedbool = true;
TempChar = hit.collider.gameObject;
}
Answer by frederik_krogh · Sep 18, 2020 at 06:29 PM
same problem.. If you could read the aspect ratio u could add the error margin to the mouse pointer and get the right result..
I mean, my native resolution is 1920x1200 but i force the player to go 1920x1080 in the player settings. I can see that i uses raycast for the native resolution (1920x1200) instead of the players resolution. This means that when i push a button the raycast returns a slightly offset position. I can get the right value if i add the extra valu to the mouseposition.. It must be a bug.. I think unity reads the mouseposition directly from the machine and puts it into the player without transfor$$anonymous$$g it even if the player is scaled. $$anonymous$$akes no sense
Your answer
Follow this Question
Related Questions
Restricting ratio of a 2D Game 2 Answers
Resolution 2 Answers
LOD - Shape of boundary for transition LOD0/LOD1/LOD2/ 0 Answers
Whats the best practice to draw GUI control buttons? 0 Answers
How do I remove the extra tiles in the right and bottom 0 Answers