- Home /
How to get mouse cursor in Vector3
I'm trying to make a script that gives Force to the object of position "X" in the direction of mouse cursor.
public Transform start;
public Vector3 end; //end = mouse cursot.
GetComponent<Rigidbody>().AddForce(end * speed);
Such as to shoot an arrow from a bow. Bow is the "start" and aim mouse cursor. I tried everything I found, but nothing Is returned the exact coordinates of the mouse in vector3. In the first person is easy, but I can not do it in the third person.
This work in first person
if (Input.GetButtonDown("Fire1"))
{
Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hitInfo;
if (Physics.Raycast (rayOrigin, out hitInfo))
{
GetComponent<Rigidbody>().AddForce(rayOrigin.direction * shotSpeed);
}
But in third person Ray start from camera and "rayOrigin.direction" no more pointing from Object to cursor. How to replace "Camera.main.ScreenPointToRay" with position of my object?
I don't understand the question. Good English communication skills are essential for the English UA board.
How about a diagram or two to help your explanation? Diagrams can help overcome the language barrier.
Answer by hbalint1 · Apr 21, 2015 at 06:22 PM
maybe:
if (Input.GetButtonDown("Fire1"))
{
RaycastHit hitInfo;
Vector3 mousePos = Input.mousePosition;
mousePos.z = 10; // select distance = 10 units from the camera
if (Physics.Raycast (yourObject.position, mousePos, out hitInfo))
{
GetComponent<Rigidbody>().AddForce(rayOrigin.direction * shotSpeed);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613193627im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mouse cursor texture change 0 Answers
using mouse cursor to change levels 0 Answers
mouse shape not ok 0 Answers
Mouse cursor problem exporting in windows store 0 Answers
Mouse Coursor hide/show 1 Answer