Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by deadsea2004 · Jul 04, 2018 at 04:11 AM · 2d gamemousepositiontop down shooterinvert

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 19, 2020 at 12:37 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deadsea2004 · Jul 07, 2018 at 06:21 AM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image deadsea2004 · Jul 07, 2018 at 06:23 AM 0
Share

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?

avatar image MPHYS deadsea2004 · Jul 07, 2018 at 07:54 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I move camera towards the mouse while anchoring it to the player? 0 Answers

shotgun like shooting 1 Answer

I need help with a top down 2d shooter 3 Answers

Adding force to mouse position adds force relative to screen center 0 Answers

Top down 2d shooter enemy control 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges