1
\$\begingroup\$

I have a camera which is showing the userInterface (canvas) object into the front of my camera. I am only updating my userinterface position if the camera raycast is not hitting my userInterface object collider. something like this

public class UIPlacerAtCamFront : MonoBehaviour {

    public GameObject userInterface;
    public float placementDistance;
    Vector3 uiPlacementPosition;
    RaycastHit  objectHit; 

    void Update () {

        Vector3 fwd = this.transform.TransformDirection(Vector3.forward);
        //Debug.DrawRay(this.transform.position, fwd * 50, Color.green);

        if (Physics.Raycast(this.transform.position, fwd, out objectHit, 50))
        {
            //raycast hitting the userInterface collider, so do nothing, userinterface is infornt of the camrea already
        }
        else
        {
            //raycast is not hitting userInterfae collider, so update UserInterface position accoding to the camera
            uiPlacementPosition = this.transform.position + this.transform.forward * placementDistance;
            userInterface.transform.position = uiPlacementPosition;
        }

        //Continuously update userInterface rotation according to camera
        userInterface.transform.rotation = this.transform.rotation; 
    }
}

The above script has attached with my camera, it displaying the object correctly but as i start to rotate my camera my UI object rotation looks very strange as below image suggested This is inital correct rotation

As i rotate, this problem occurs enter image description here

I know that the problem is in rotation, so I tired to change my this rotation code

    userInterface.transform.rotation = this.transform.rotation; 

to this

userInterface.transform.localEulerAngles = new Vector3 (this.transform.localEulerAngles.x,
            0,
            this.transform.localEulerAngles.z);

but it bring another strange rotation for me, like given below enter image description here

I want that my userinteface object face my camera correclty, even my camera watching the start or end of my userinteface object. How can i rotate my UI according to camera rotation correctly?

\$\endgroup\$
  • \$\begingroup\$ Why are you using a worldspace canvas to immitate a screenspace canvas? Just set the canvas to be screenspace. \$\endgroup\$ – Draco18s Apr 18 '17 at 19:28
  • \$\begingroup\$ @Draco18s yes you are right but i am making some selection on canvas with camera pointer that why i can't set canvas to screen space. The camera pointer will rotate according to camera and it will make selection from world space UI. \$\endgroup\$ – Muhammad Faizan Khan Apr 19 '17 at 4:31
  • \$\begingroup\$ Why not just make it a child object of the camera, it'll turn and move with the camera without additional scripting. \$\endgroup\$ – John Hamilton Apr 19 '17 at 6:13
  • \$\begingroup\$ @JohnHamilton refer to my previous comment "yes you are right but i am making some selection on canvas with camera pointer that why i can't set canvas to screen space. The camera pointer will rotate according to camera and it will make selection from world space UI. " \$\endgroup\$ – Muhammad Faizan Khan Apr 19 '17 at 6:19
  • \$\begingroup\$ @JohnHamilton if i make it child then it will roate with the parent movement and i will unable to select userinteface object from my camera pointer object \$\endgroup\$ – Muhammad Faizan Khan Apr 19 '17 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.