[Solved] - Different game mouse position problems

Unity version: 5.5.0f3

I’m still in the very beginning of this game, but basically I’m just trying to get the bright center to coincide with the mouse’s movements (Like you’re looking through the scope of a gun).

To start, I was going to follow Ben’s “movement by mouse” lecture, and try and add the y axis myself afterwards. But even copying Ben’s code exactly, the scope doesn’t coincide with my mouse movements. When I realized my units and screen width were different, I tried changing things to fit, but it still doesn’t work. The scope is still offset to my mouse position. Here is my game, and an image of my code.

StealthMode.zip (5.7 MB)

I would really appreciate it if someone could help get it working, and also edit it so the y-axis works as well.

I’ll grab it down and have a look. In future it’s really useful if you could commit your code up to GitHub or BitBucket, that way we can either view it online or pull it locally without having to mess about with dodgy zip files.

try this:

using UnityEngine;

public class CrossHairs : MonoBehaviour {

	private Vector3 _crosshairPos;
	private Vector3 _mousePos;
	void Start () {
		_mousePos = new Vector3(0,0,0);
	void Update () {
        _mousePos.x = Input.mousePosition.x;
		_mousePos.y = Input.mousePosition.y;
		_mousePos.z = transform.position.z - Camera.main.transform.position.z;
        _crosshairPos = Camera.main.ScreenToWorldPoint(_mousePos);
		transform.position = _crosshairPos;


It’s possible to do it your original way but you have to adjust for screen size and resolution and it gets messy. By doing it with Camera.main.ScreenToWorldPoint you’re converting from Screen Space (mouse) to World Space (crosshair) which is exactly what you want to do, no mucking about.

Thanks a lot @ninjachimp! And I will try to commit my code to GitHub or BitBucket next time, sorry for the hassle. I promise I’m not trying to give you a virus. ;D

1 Like

Privacy & Terms