Store and use current mouse position properly
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!
Yay I made it. But I'm facing another Problem now :( The projectile is created as supposed but it never reaches its destination. It kind of "drops" in front of the player. I've tried to add force and velocity to the rigidbody but it didn't solve my problem. Do you have any suggestions for me? Thank you very much in advance folks.
Answer by DominikHeyn · Sep 17, 2016 at 04:34 PM
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
}
Answer by toddisarockstar · Sep 15, 2016 at 07:58 PM
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.