Hi everyone - I am reasonably new to Unity and C# but I’m finding my feet on an e-learning demo and trying to pack in as many self-objectives to learn on it to get to grips with both language and program.
The aim of the demo is to walk around and click on various items to carry out tasks. Currently I have the following code to raycast out to the mouse pointer when it is clicked and detect which object was clicked:
// test on every update if mouse has been left clicked on a gameObject
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
Debug.Log("Main camera pixelWidth = "+Camera.main.pixelWidth);
Debug.Log("Main camera pixelHeight = "+Camera.main.pixelHeight);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float distance = (this.InteractionDistance < 0) ? Mathf.Infinity : this.InteractionDistance;
if (Physics.Raycast(ray, out hit, distance)){
TransformClicked(hit.transform);
}
}
And this works. A little awkward because the user has to reposition the mouse to click, but it works.
So I decided after some play testing to change the mouseclick to a button press, using E, like on many FPS like Half Life.
if(Input.GetKeyDown("e")){ // use e click interact, more Half Life like
Debug.Log("Main camera pixelWidth = "+Camera.main.pixelWidth);
Debug.Log("Main camera pixelHeight = "+Camera.main.pixelHeight);
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width/2, Screen.height/2)); //ray to centre of the screen
float distance = (this.InteractionDistance < 0) ? Mathf.Infinity : this.InteractionDistance;
if (Physics.Raycast(ray, out hit, distance)){
TransformClicked(hit.transform);
}
}
And again this works great - a ray is sent to the centre of of the screen but for me this is way to precise. What would be perfect would be if you could resize the size of the ray so instead of being a pixel or whatever it is, it was 50 x 50 or something.
Not really knowing if this was possible, I’ve searched online and set out to determine an area at the centre of the screen that is the “ray area” - if anything inside this area hits a gameobject then run the TransformClicked method.
So far I have worked out 4 Vector2 points on screen using
private void CreateClickPoint()
{
//work out center points for screen x and y
centerScreenX = Screen.width/2;
centerScreenY = Screen.height/2;
//work out a clickable area
xTopLeft = new Vector2(centerScreenX-30, centerScreenY-30); Debug.Log("xTopLeft = "+xTopLeft);
xTopRight = new Vector2(centerScreenX+30, centerScreenY-30); Debug.Log("xTopRight = "+xTopRight);
xBottomLeft = new Vector2(centerScreenX-30, centerScreenY+30); Debug.Log("xBottomLeft = "+xBottomLeft);
xBottomRight = new Vector2(centerScreenX+30, centerScreenY+30); Debug.Log("xBottomRight ="+xBottomRight);
}
And that’s it. Is there anyway for me to use these 4 points and say When E is pressed, determine using rays if there is a game object within the area of these 4 points? Or have I missed something that is built in that is extremely obvious?
cheers for any help
Frank