- Home /
how to make a cross hair that follows the exact position of the mouse
hey guys, i"m trying to make a crosshair that is following the position of the mouse cursor on the screen because i'm making a 2.5D shooting game ,i use a script to get only X and Y position of the mouse .my character's gun is looking at this crosshair so when i shoot ,the bullet hits the point where the crosshair is. my problem is that the cross hair is not 100% accurate in following the exact position of the mouse. how do i attach my cross hair to exact position of mouse on screen? i made a GIF file to show the issue : http://www.mediafire.com/view/5qd1pp6qmaq35vg/GIF_20180917_000015.gif
here is my script to look at mouse position only on x and y :
private Vector3 targetPos;
public float speed = 2.0f;
void Start() {
targetPos = transform.position;
}
void Update () {
float distance = transform.position.z + Camera.main.transform.position.z;
targetPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
targetPos = Camera.main.ScreenToWorldPoint(targetPos);
Vector3 followXonly = new Vector3(targetPos.x, targetPos.y, transform.position.z);
transform.position = Vector3.Lerp (transform.position, followXonly, speed * Time.deltaTime);
}
}
have you guys heard about a game "my friend pedro"? i want a crosshair exactly like that. like this photo :
https://i.ytimg.com/vi/s2$$anonymous$$pyXb9XRk/hqdefault.jpg
where ever the cross hair looks his hand aim ik looks too.by the way i used to use an UI image for the cross hair on te screen and attached a script to it and told it to be where ever the mouse cursor is.the problem was that an image does not have a transform component so i couldn't use an image as a target for my ai$$anonymous$$g hand ik
Answer by hexagonius · Sep 16, 2018 at 08:44 PM
You're lerping the position. Each frame the position the crosshair is set to is somewhere between it's current position and followXonly. Just set transform.position to followXonly and you're good to go.
thanks for answer, you mean something like this? transform.position = followXonly;
To clarify a bit, let's say you're moving from 0 to 120 (on the X-axis), and moving 50% of the way from your current position per frame.
Frame # | New X Position
0 | 0
1 | 60
2 | 90
3 | 105
4 | 112.5
No matter how long you move, you'll never completely reach the destination. With that in $$anonymous$$d, putting the cursor directly at the mouse position (followXonly) is a perfectly reasonable approach.
If you still want to have the crosshair ease towards the cursor position a bit if the mouse moves especially quickly, you might want to look into Vector3.$$anonymous$$oveTowards() to define a strict maximum rate of movement.
so i changed the script to something like this and still the cross hair doesn't exactly match with mouse position :( idont know whats wrong.
transform.position = Vector3.$$anonymous$$oveTowards(transform.position, followXonly, speed * Time.deltaTime);
thanks guys for your advice . so i changed the script and used only Vector2.$$anonymous$$oveTowards and it works fine .but just there is a tiny little issue with the way the cross hair moves on the screen. see here :
http://www.mediafire.com/view/nwhsy3dw9ajggin/GIF_20180918_111600.gif
it's movement is a little bit robotic and not as smooth as before but after all i got want i needed thanks :)))
Your answer
![](https://koobas.hobune.stream/wayback/20220612174736im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Third Person Cross Hair Question 1 Answer
C# Crosshair 4 Answers
Use Gamepad right analog stick instead of mouse to control crosshair movement 3 Answers
C# OR Java- Multiplayer Shoot Sound 0 Answers
2D Aiming with a mouse angle problem 1 Answer