• 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 GKiller · Oct 22, 2017 at 06:40 PM · rotationpositionrotation axis

Gameobject rotation breaks children position

I'm trying to make a tetris game. However I'm having troube with rotating and element and the offset.

The lower left corner of the grid is (0,0).

alt text

When rotating 90°, the position of LowerL doesn't change as you can see

alt text

And when trying to go over the edge, it allows me to do so because the children positions are wrong by +1x.

This is the Piece code

 public class Piece : MonoBehaviour
 {
     float fall = 0;
     GameController gameController;
 
     private void Start()
     {
         gameController = FindObjectOfType<GameController>();
     }
 
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.RightArrow))
         {
             Move(new Vector3(1f, 0f, 0f));
         }
         else if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             Move(new Vector3(-1f, 0f, 0f));
         }
         else if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             Rotate(new Vector3(0f, 0f, 90f));
         }
         else if (Input.GetKeyDown(KeyCode.DownArrow) || Time.time - fall >= 1)
         {
             fall = Time.time;
             Move(new Vector3(0f, -1f, 0f));
         }
     }
 
     bool CheckPositionIsValid()
     {
         foreach (Transform block in transform)
         {
             if (!gameController.CheckInGrid(gameController.Round(block.position)))
             {
                 return false;
             }
         }
 
         return true;
     }
 
     void Move(Vector3 moveDirection)
     {
         transform.position += moveDirection;
 
         if (!CheckPositionIsValid())
         {
             transform.position += moveDirection * -1.0f;
         }
     }
 
     void Rotate(Vector3 rotation)
     {
         transform.Rotate(rotation);
         if (!CheckPositionIsValid())
         {
             transform.Rotate(rotation * -1.0f);
         }
     }
 }
 

and the GameController code:

 public class GameController : MonoBehaviour
 {
     public static int gridWidth = 10;
     public static int girdHeight = 20;
 
     public bool CheckInGrid(Vector2 position)
     {
         if (position.x < 0 || position.x > gridWidth || position.y < 0)
         {
             return false;
         }
 
         return true;
     }
 
     public Vector2 Round(Vector2 position)
     {
         return new Vector2(Mathf.Round(position.x), Mathf.Round(position.y));
     }
 }

What is it that I'm missing or am doing wrong in?

Thank you for your help.

grid1.png (191.2 kB)
grid2.png (183.7 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GKiller · Oct 23, 2017 at 05:54 AM

My problem was caused by the fact that my pivot point was centered. Changing the splicing solved this for me.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 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

Related Questions

Player not facing the mouse correctly 1 Answer

Problem when i rotate a 2d object aroun Z-axis 1 Answer

How do you make a Tps cam with RotateAround around the X and Y axis without rotating on the Z axis? 1 Answer

How to move camera up and down 1 Answer

Why is the animator not changing the position but only changing the rotation? 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