Preventing world interactions when using an UI element with an Orthographic 3D camera
Hey all,
Well, I searched every tiny corner of google but couldn't find an answer. My problem is simple, I want to make a rotating player towards the mousePosition (when clicking on the screen), it does not have to be smooth so I use LookAt(). I also have 2 big buttons in my game, one is for shooting. The problem is, if I click on this button the player will 'look at' the button and shoot at it. This is not supposed to happen.
For example, the player sees an enemy at the top of the screen, he rotate towards that enemy and clicks 'Shoot', now the Player will rotate towards this GUI element and NOT towards the location of the enemy.
This is my code:
Camera camera;
GameObject player;
void Awake()
{
camera = GetComponent<Camera>();
player = GameObject.Find("Player");
}
void Update()
{
if (Input.GetMouseButton(0))
{
player.transform.LookAt(GetWorldPositionOnPlane(Input.mousePosition, 1));
}
}
public Vector3 GetWorldPositionOnPlane(Vector3 screenPosition, float z)
{
Ray ray = Camera.main.ScreenPointToRay(screenPosition);
Plane plane = new Plane(Vector3.up, new Vector3(0, 1.5f, 0));
float distance;
plane.Raycast(ray, out distance);
return ray.GetPoint(distance);
}
I create a plane so the player can rotate towards the mousePosition, now I can't use EventSystem because the Plane doesn't have any script attached.
Does someone has a solution for this?
Thanks in advance.
Your answer
Follow this Question
Related Questions
Finding the gameobject closest to the centre of the screen (WorldToViewportPoint) 0 Answers
Orthographic camera size not changing 0 Answers
How to Map a RenderTexture of a Plane by Native Code 0 Answers
Orthographic Camera Appears to be Only Rendering Top Half of the Screen? 0 Answers
Orthographic Camera for objects but perspective for GUI? 1 Answer