- Home /
Synchronizing Fake Mouse Position with Actual Mouse Movement
Hello,
I'm working on a 2D top down shooter game, the controls are very similar to hotline miami. When I started on the aiming part I simply used Input.mousePosition, however this led to some difficulties.
When locking on to an enemy I had a texture, which normally has the same position on screen as the mouse, stay on that enemy until it died or it was no longer in sight. This meant that the mouse could move freely but the texture stayed on the enemy. As soon as the lock was broken the texture would shoot back to the actual mouse position and this could lead to a big sudden change in aim direction.
To combat this situation I locked the cursor instead of having it confined and used the following code to determine the position of the texture:
public Vector2 fakeMousePos;
private void Start() {
fakeMousePos = new Vector2(Screen.width / 2, Screen.height / 2);
}
private void Update() {
float mouseX = Input.GetAxisRaw("Mouse X");
float mouseY = Input.GetAxisRaw("Mouse Y");
fakeMousePos += new Vector2(mouseX, mouseY);
if (fakeMousePos.x > Screen.width) fakeMousePos.x = Screen.width;
if (fakeMousePos.x < 0) fakeMousePos.x = 0;
if (fakeMousePos.y > Screen.height) fakeMousePos.y = Screen.height;
if (fakeMousePos.y < 0) fakeMousePos.y = 0;
}
I set the sensitivity of the Mouse Axis to be 1 because I thought this would mean that it would move at the same speed as it did in the operating system, this is not the case unfortunately.
I'm aware there is some kind of plugin that can change the mouse position in Unity, but I dont really want to use it unless I really have to. I have looked into the StandaloneInputModule, but I think this would just work when working with UI and a fake mouse position.
I would love to hear any ideas and possible solutions to the matter!
Thanks in advance,
Hybris
Your answer
Follow this Question
Related Questions
dash towards mouse position? (New Input System) 2 Answers
Problem with Vector3 Distance and Mouse Input, 1 Answer
How can I stop getting mouse position of Ui Elements? 0 Answers
How to check if mouse is above or below player? 2 Answers
Adding an offset to instantiated object based on mouse distance from player 1 Answer