Hey folks,
I’m struggling with storing and using the current mouse position for days now.
I’m working on a “mini MOBA game” and I want to create a skillshot ability where the player selects the ability first and then the direction (or position) of its determination.
I’ve played around with this line of code:
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
But it stores the x axis only (in my case). Technically I need the x and the z axis to achieve this.
On a further click the created Gameobject should move to the selected position, obviously.
Do you have any tips or code examples for me?
Or do you have another advice how to solve this?
Thank you very much in advance!
first of all mouse position doesnt have a “Z”. its two dimentional as X and Y.
its a Vector2.
so to just get the mouse position you would say:
public float mx;
public float my;
void Update () {
mx = Input.mousePosition.x;
my = Input.mousePosition.y;
}
keep in mind that mouse results are upside down from GUI coordinates.
if you are looking for what the mouse is on top of in three dimentional space then you need to raycast from the camera into the scene and see what it hits.
Thanks for your answer.
I figured it out that this is just a Vector2.
I solved this problem by coding this:
Plane plane = new Plane(Vector3.up, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
And then continuing with an if statement:
if (plane.Raycast (ray, out point))
{
// do stuff
}