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 /
avatar image
0
Question by pil95 · Mar 09, 2021 at 12:49 PM · tilemapmatrixmatrix4x4

Using Matrix4x4.TRS to rotate Tilemap

Hi, I'm working on a 2D game and trying to rotate a section of my Tilemap 90 degrees on the Z-axis via using the Tilemap.SetTransformMatrix function. From building on the sample code and Matrix4x4.TRS from the scripting API I can get the selected area of the Tilemap to rotate, but the tiles are rotating individually in place instead of as a group as I wanted.

  Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, 90f), Vector3.one);
  //code gets list of vector3int locations for all the square region of tiles to get rotated
  //then foreach location found:
  tilemap.RemoveTileFlags(location, TileFlags.LockTransform); //remove lock so tiles can be rotated
  tilemap.SetTransformMatrix(location, matrix); //apply matrix

I've found lots of Tilemap tutorials, but none have discussed group rotation of tiles, just singular ones. I would be very happy if someone can tell me what I am doing wrong.


Update: I wrote this horrible proof of concept code while testing which rotates the 3x3 center section of my tilemap 90 degrees clockwise as a group, without affecting the rotation value of the tilemap itself which is what I wanted:


There has to be an easier/much more efficient way to do this?

 Matrix4x4 matrix = Matrix4x4.TRS(new Vector3Int(2, 0, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(-1, 1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(0, 2, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(-1, -1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(-2, 0, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(1, -1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(0, -2, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(1, 1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(-1, 0, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(2, 1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(1, -1, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(0, 1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(1, 1, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(-1, 0, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(-1, 1, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(0, -1, 0), matrix); 
         matrix = Matrix4x4.TRS(new Vector3Int(-1, -1, 0), Quaternion.Euler(0f, 0f, -degreesRotated), Vector3.one);
         tmap.SetTransformMatrix(new Vector3Int(1, 0, 0), matrix);




Comment
Add comment · Show 2
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 andrew-lukasik · Mar 07, 2021 at 09:30 PM 0
Share

try tilemapTransform.rotation = Quaternion.Euler(0f, 0f, 90f); instead

avatar image pil95 andrew-lukasik · Mar 07, 2021 at 09:58 PM 0
Share

Hi thanks for the reply, this was the method I was originally considering, however it rotates the entire tilemap and not just the selected area, which in turn affects all the code for player movement.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Namey5 · Mar 07, 2021 at 11:34 PM

I'm not familiar with tilemaps specifically, but if you can rotate and reposition the tiles then you should have everything you need. The basic idea would be to set the rotation of the tiles to what you desire, then rotate their positions around the centre of the transformation by the rotation;

 Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, 90f), Vector3.one);
 
 //Assuming this rotates the tiles correctly
 tilemap.SetTransformMatrix (location, matrix);
 
 //Then, rotate each tile's position using the transform matrix;
 foreach (tile in tilemap)
     tile.position = matrix.MultiplyPoint (tile.position);
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 pil95 · Mar 08, 2021 at 06:02 PM 0
Share

Appreciate the reply, from my experience it's impossible to set the position of the tiles directly using something like tile.position = matrix.MultiplyPoint (tile.position); which is not recognized by Unity. I've only been able to manage this by modifying the matrix which is being passed to tilemap.SetTransformMatrix(location, matrix)

Eg I modify my code to Matrix4x4 matrix = Matrix4x4.TRS(Vector3.up, Quaternion.Euler(0f, 0f, 90f), Vector3.one); It will rotate all tiles 90 degrees and also increase their Y position value by 1 for each tile

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

117 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

Related Questions

How to get translation from matrix? 1 Answer

Efficient code for shortest distance inside a fragment shader 1 Answer

Why Matrix4x4.MultiplyPoint does row-major access while Matrix4x4 is column-major? 1 Answer

Creating a Matrix4x4 from an Object's Transform 1 Answer

How to get the inverse of a transformation matrix that has a scale of zero? 1 Answer


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