- Home /
How do I invert mouse movement for a specific section of my game?
Hello! Newcomer to Unity here, seeking some advise!
Ok, so I have this 2D top-down shooter, where I have a script attached to a crosshair object. On Update, this crosshair will move according to Input.mousePosition, hence if my cursor moves, so does this crosshair.
I have this special level where I wish to invert the mouse input, so players will have to aim with everything reversed. Here's the code on my crosshair:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crosshair : MonoBehaviour
{
// Use this for initialization
void Start ()
{
//Hide mouse cursor
Cursor.visible = false;
}
// Update is called once per frame
void Update ()
{
transform.position = Input.mousePosition;
}
}
Does Unity have a way to invert mouse input for a single level? Or is there anything I could do to achieve this? If I move to the next level, the mouse input should return to normal.
Thanks!
Answer by Khafeine · Feb 19, 2020 at 10:02 AM
Hey guys don't you even math? @deadsea2004 Use this one to get reversed position and assign it to cursor's transform position like this
transform.position=Reverseposition(input.mouseposition);
Vector2 ReversePosition(Vector2 uipos)
{
uipos.x = Screen.width - uipos.x;
uipos.y = Screen.height - uipos.y;
return uipos;
}
Even it's a bit rude, you're right -.-
The origin of the mouse coordinates are in screen space coordinates. The point (0,0) is in the lower left corner of the screen. The top right corner is the point (Screen.width, Screen.height).
If you just mulitply your position by -1, you just flip the coordinates into the negative range which is completely off-screen (to the left of the left edge of the screen and below the bottom edge).
Classical inversion of the vector would work if the origin is at the screen center. So you could also do
Vector2 ReversePosition(Vector2 pos)
{
Vector2 scrCenter = new Vector2(Screen.width*0.5f, Screen.height*0.5f);
pos -= scrCenter; // shift origin to the center
pos = -pos; // invert
return pos + scrCenter; // shift back
}
Though this would result in the same thing if you combine all together and simplify
pos = (-(pos-scrCenter))+scrCenter
pso = (-pos +scrCenter)+scrCenter
pos = -pos + 2*scrCenter
pos = 2*scrCenter - pos
Since scrCenter is half the screen width / height, two times the center is just the full screen width and height. So the result is the same what $$anonymous$$hafeine wrote here.
Answer by Skylander17 · Jul 04, 2018 at 04:46 AM
You should reverse it manually like this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crosshair : MonoBehaviour
{
isInverted = false;
// Use this for initialization
void Start ()
{
//Hide mouse cursor
Cursor.visible = false;
}
// Update is called once per frame
void Update ()
{
if(isInverted)
{
transform.position = -Input.mousePosition;
}
else
{
transform.position = Input.mousePosition;
}
}
}
if you change isInverted variable to true somewhere in game, it will work.
Hm, if I multiply Input.mousePosition by -1, this causes my Crosshair image to vanish for some reason. I'm assu$$anonymous$$g it went off-screen?
Answer by MPHYS · Jul 04, 2018 at 04:55 AM
I would suggest you go about it something like this:
using UnityEngine;
using UnityEngine.SceneManagement;
public class Crosshair : MonoBehaviour
{
private bool reverseLevel = false;
// Use this for initialization
void Start ()
{
//Hide mouse cursor
Cursor.visible = false;
if(SceneManager.GetActiveScene().name == " scene name where it reverses")
{
reverseLevel = true;
}
}
void Update()
{
if(reverseLevel)
{
transform.positon = ReverseCrossHair(Input.position);
}
else transform.position = Input.position;
}
public Vector2 ReverseCrossHair(Vector2 pos)
{
return -pos;
}
}
The best way I can see you reversing your crosshairs is by multiplying it by -1 since this reverses the coordinates.
Edit: You can just copy paste this code and it should work. Since we change the bool to true only at the level you want, this code can still be reused for your other levels.
Hm, if I multiply Input.mousePosition by -1, this causes my Crosshair image to vanish for some reason. I'm assu$$anonymous$$g it went off-screen?
Can't imagine why that happened but you can check if went off screen by checking the Transform. Compare the it's postion to where it was when you started. Then you can figure out what went wrong with math. Debug.Log() is a life saver.