Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by Jaime97 · Nov 29, 2021 at 05:31 PM · unity 2dtilemapmousepositionisometricmouse-drag

Rotate and scale sprite based on mouse position

Hello,

I'm doing some kind of RTS and I need to scale and rotate a sprite based on mouse position. Think of it as something like a common RTS selection, where we click a point of the screen and drag to show a selection rect, but instead of the two points being the upper left corner of the rect and the bottom right, I want them to be the upper left and the upper right, and define height by other parameters (all of this is to create a formation system for units, like the one in Total War). I also want to rotate the rect based on the mouse position.

This is what I've achieved right now: alt text alt text

So, it is working as expected. This is the code:

 private void Update()
     {
         if (Input.GetMouseButtonDown(1))
         {
             selectionStartPosition = MousePositionManager.GetMouseWorldPosition();
             formationAreaGameObject.SetActive(true);
         }
 
         if (Input.GetMouseButton(1))
         {
             var mousePosition = MousePositionManager.GetMouseWorldPosition();
 
             float scaleX = Vector2.Distance(mousePosition, selectionStartPosition);
             formationAreaGameObject.transform.localScale = new Vector3(scaleX, 1, 1);
 
             Vector3 centerPos = new Vector3(selectionStartPosition.x + mousePosition.x, selectionStartPosition.y + mousePosition.y) / 2;
             formationAreaGameObject.transform.position = centerPos;
 
             var rotationPosition = new Vector2(mousePosition.x - centerPos.x, mousePosition.y - centerPos.y);
 
             var angle = Mathf.Atan2(rotationPosition.y, rotationPosition.x) * Mathf.Rad2Deg;
             formationAreaGameObject.transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
         }
 
         if (Input.GetMouseButtonUp(1))
         {
             formationAreaGameObject.SetActive(false);
         }
     }

Now, my current problem is that I'm working on a isometric 2D tilemap scene, so I would want this "formation area" to be align with the map. To do that, it needs to be 60 degrees rotated in the X axis, so I've tried changing this line:

 formationAreaGameObject.transform.rotation = Quaternion.Euler(new Vector3(60, 0, angle));

But this mess up all the other logic: the position of the rect changes when it should stay fixed to the initial mouse click position, it doesn't fully rotate to the mouse position point... And I understand why, but I really don't know how to fix the code to work with this 60 degrees variation. Can somebody help?

captura-de-pantalla-16.png (10.0 kB)
captura-de-pantalla-17.png (9.2 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

183 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 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 Can I Fix it ?? (Isometric Tilemap Twist) 0 Answers

Isometric Tilemap with multiple Z layers 0 Answers

What is causing this issue with Tilemap.SetTile and Update method? 0 Answers

Checking OnCollisionEnter2D names with TilemapCollider2D 0 Answers

Major Tileset Glithing 0 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