How to rotate an object using code?

Explanation:

Hello, I am new to the world of game making and coding in general. I have looked up lots of tutorials but a lot them either didn’t give enough information or the code just didn’t work for some reason.

I am making a Tetris game and I was following a video tutorial until I came across one error that I didn’t find a solution to. When I push play it just keeps rotating in the x position. Its supposed to just rotate the shape once when you hit the key just like Tetris.

transform.RotateAround (transform.TransformPoint (rotationPoint), new Vector 3 (0, 0 ,1), 90);

If (!ValidMove())

   transform.RotateAround (transform.TransformPoint (rotationPoint), new Vector 3 (0, 0 ,1), -90);

Sounds like you probably didn’t get enough of the context from whatever tutorial you followed.

That code snippet isn’t very helpful. It doesn’t have enough context (when does this code execute?), and it looks like you probably re-typed or paraphrased your code instead of pasting the actual code from your project (e.g. you have “If” with a capital-I rather than “if”, which I’m pretty sure won’t even compile). When requesting debugging help, you should always copy and paste code that you’ve actually run in Unity, to ensure you aren’t accidentally introducing or removing errors that could affect the advice you receive.

But taking a stab in the dark, I’m guessing this code is in a function that Unity automatically runs every frame, such as Update. To make it only happen when the user presses a button, you need to put it inside an “if” block that checks for the appropriate user input. That would probably use Input.GetKeyDown for the condition, although there’s a few different ways of getting input.

As a side note, I’ll point out that you probably don’t want to rely on any physics stuff when building Tetris. If you try to have colliders and gravity and the like, it’ll be tough to get your pieces to line up or lie flat. (It’s not completely clear from your post whether you are or not, but if you were writing your own grid-based logic then it would be mildly surprising that your rotation works entirely on the level of Transforms.)