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.
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.
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.