Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Daniloleemes · Aug 01, 2020 at 01:20 PM · rotationvector33d models

Transform.Rotate not working as expected

Hi all, I'm trying to rotate 4 turrets but one of them is not behaving accordingly. Here's what is happening (notice how the bottom right one is different)

I tried debugging and it seems that via code the Rotation can't have negative values and thus my logic kinda breaks, because in the bottom left case I'd like it to rotate from 0 to -90 on Y axis. How could I achieve that?

Here's my code:

 void FixedUpdate() {
             float currentRotationY = cannonBase.transform.eulerAngles.y * (cannonBase.transform.rotation.y < 0 ? -1 : 1);
 
             switch (currentPosition) {
                 case WeaponPosition.BOTTOM_LEFT:
                     RotateCannon(currentRotationY);
                     break;
                 case WeaponPosition.TOP_LEFT:
                     RotateCannon(currentRotationY - 90);
                     break;
                 case WeaponPosition.TOP_RIGHT:
                     RotateCannon(currentRotationY - 180);
                     break;
                 case WeaponPosition.BOTTOM_RIGHT:
                     RotateCannon(currentRotationY);
                     break;
                 default:
                     break;
             }
         }
 
         private void RotateCannon(float currentRotationY) {
             int maxCannonAngle = 90;
             int minCannonAngle = 0;
 
             if (currentRotationY <= minCannonAngle) animationDirection = AnimationDirection.FORWARD;
             if (currentRotationY >= maxCannonAngle) animationDirection = AnimationDirection.BACKWARD;
 
             if (currentRotationY < maxCannonAngle && animationDirection == AnimationDirection.FORWARD) {
                 cannonBase.transform.Rotate(Vector3.up * (rotationSpeed * Time.deltaTime));
             } else if (currentRotationY > minCannonAngle && animationDirection == AnimationDirection.BACKWARD) {
                 cannonBase.transform.Rotate(Vector3.down * (rotationSpeed * Time.deltaTime));
             }
         }

And this is how I spawn the turrets and terrain:

 private int[] possibleRotations = new int[4] { 0, 90, 180, 360 };
         private int[] weaponRotations = new int[4] { 0, 180, 0, 180 };
 
         private List<GameObject> weapons = new List<GameObject>();
 
         // Start is called before the first frame update
         void Start() {
             instance = this;
             var whichWeapon = weaponPrefabs[Random.Range(0, weaponPrefabs.Count)];
             for (int x = 0; x < gridSize; x++) {
                 for (int z = 0; z < gridSize; z++) {
                     GameObject floorTile;
                     if (x == 0 && z == 0 || x == 0 && z == gridSize - 1 || x == gridSize - 1 && z == gridSize - 1 || x == gridSize - 1 && z == 0) {
                         var weapon = Instantiate(whichWeapon, gameObject.transform);
                         floorTile = Instantiate(cornerFloorPrefab, gameObject.transform);
                         weapon.transform.Translate(new Vector3(x, 0.5f, z));
                         weapons.Add(weapon);
                         weapon.transform.Rotate(new Vector3(0, weaponRotations[weapons.Count-1], 0));
                     } else {
                         floorTile = Instantiate(floorPrefabs[Random.Range(0, floorPrefabs.Count - 1)], gameObject.transform);
                     }
                     floorTile.transform.Translate(new Vector3(x, 0, z));
                     floorTile.transform.Rotate(new Vector3(0, possibleRotations[Random.Range(0, possibleRotations.Length - 1)], 0));
                 }
             }
 
             for (int x = 0; x < gridSize; x++) {
                 for (int z = 0; z < gridSize; z++) {
                     var floorTile = Instantiate(floorPrefabs[Random.Range(0, floorPrefabs.Count)], gameObject.transform);
                     floorTile.transform.Translate(new Vector3(x, -1, z));
                 }
             }
         }


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

196 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 avatar image 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

Creating the After Effects Wiggle expression to effect rotation in C# 1 Answer

Aligning/rotating vectors 1 Answer

Why does this code not work? 1 Answer

How to calculate the rotation of a hexagon based on its six vertices position 2 Answers

How to get a proper ramming effect? 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