# Question Why does one rotation work, while the other doesnt?

Discussion in 'Scripting' started by Quidney, Nov 30, 2023.

1. ### Quidney

Joined:
Sep 8, 2020
Posts:
10
The parts you should Focus on are: Jumpin() and Sword()
I didn't cut LookingAngle() off because it has to do with rotation and Idk if it affects the wing, but considering it doesn't affect sword, I would say it shouldn't..?

Code (CSharp):
1.
2. using UnityEngine;
3.
4. public class BirdScript : MonoBehaviour
5. {
6.
7.     public GameObject sword;
8.     private bool swordUp;
9.
10.     public GameObject birdParent;
11.     public GameObject wing;
12.     public Rigidbody2D rb;
13.     public float jumpForce = 5.0f;
14.     public float velocityUpDown;
15.
16.
17.     // Start is called before the first frame update
18.     void Start()
19.     {
20.         wing.transform.Rotate(0, 0, 45);
21.
22.         sword.transform.Rotate(0, 0, 45);
23.         swordUp = true;
24.     }
25.
26.     // Update is called once per frame
27.     void Update()
28.     {
29.         Jumpin();
30.
31.         LookingAngle();
32.
33.         Sword();
34.     }
35.
36.     void Jumpin()
37.     {
38.         if (Input.GetKeyDown(KeyCode.Space))
39.         {
40.             wing.transform.Rotate(0, 0, -90f);
41.         }
42.
43.         if (Input.GetKeyUp(KeyCode.Space))
44.         {
45.             wing.transform.Rotate(0, 0, 90f);
46.
47.             rb.velocity = new Vector2(0, jumpForce);
48.         }
49.     }
50.
51.     void LookingAngle()
52.     {
53.         velocityUpDown = rb.velocity.y;
54.
55.         if (velocityUpDown > 0)
56.         {
57.             float rotationZPlus90 = (velocityUpDown * 8) + 90;
58.             float clampedPlus90 = Mathf.Clamp(rotationZPlus90, 90, 135);
59.             transform.rotation = Quaternion.Euler(0, 0, clampedPlus90);
60.         }
61.         else if (velocityUpDown < 0)
62.         {
63.             float rotationZMin90 = 90 - ((velocityUpDown * 8) * -1);
64.             float clampedMin90 = Mathf.Clamp(rotationZMin90, 45, 90);
65.             transform.rotation = Quaternion.Euler(0, 0, clampedMin90);
66.         }
67.         else if (velocityUpDown == 0)
68.         {
69.             transform.rotation = Quaternion.Euler(0, 0, 90);
70.         }
71.     }
72.
73.     void Sword()
74.     {
75.
76.         if (Input.GetMouseButtonDown(0))
77.         {
78.            float rotationAmount = swordUp ? -90f : 90f;
79.            sword.transform.Rotate(0, 0, rotationAmount);
80.            swordUp = !swordUp;
81.         }
82.     }
83.
84.
85. }
sword and wing are both child elements of a parent object.
When I press the mouse key, and click on the sword from Hierarchy, I can see that the transform position of sword is always -45 and 45 no matter how many times I press mouseButton 0

When I press space however, the wing rotation is something random like 0 and then -18.23 then 230 etc
I have no clue why. They're both child objects, and if they both didn't work I'd think "Oh I guess Rotate() rotates them according to the world" or something, but one of them works while the other doesn't. I cannot wrap my head around it.
If it matters: Sword and wing are both empty game objects with child sprites.
For sword, the "Center/pivot" is on the left side, for wing, it is on the right side.

2. ### Quidney

Joined:
Sep 8, 2020
Posts:
10
To be clear, I know I should use "transform.localrotation" since I am setting them to a set rotation (45,-45). but I am just curious because I used Rotate() for both objects. And one of them worked so I did the same for the other object, but that one didn't work...

3. ### spiney199

Joined:
Feb 11, 2021
Posts:
7,375
If you're just snapping it to a rotation, then why not place some empty game objects, rotate them as needed, and just copy their rotations in your code?

4. ### Quidney

Joined:
Sep 8, 2020
Posts:
10
No that's what I am doing rn. As I said in my reply. I am using localroatation because it already IS in an empty game object.

But Since the sword and Wing are both in empty game objects, and they're both pivotted to the point they should rotate from, why did one work and the other didn't. That is why I created the thread.

5. ### spiney199

Joined:
Feb 11, 2021
Posts:
7,375
That's not what I said. I said create other empty game objects, and position how you need them. Then you can copy their rotation to that of your sword or wings.

There's no point dealing with all this euler stuff (when ideally you use quaternions anyway) when it's just a matter of copying and assigning rotations. K.I.S.S.

Kurt-Dekker likes this.
6. ### halley

Joined:
Aug 26, 2013
Posts:
2,292
Yeah, there are a LOT of situations where I just include a spare Empty in my objects to sort of "bookmark" an ideal position. It's simpler than storing and editing a Vector3+Quaternion, because the Editor has all the manipulator gizmos for it.

* A cannon has animated recoil. An Empty describes the resting position and the recoil position. Lerp between them.
* A hand bone is perfect for animating the hand flexing, but not ideal for holding a tool. An Empty describes the exact amount of offset when you want to hold a tool.
* A car door or seat clips through a character if they aren't aligned properly before boarding. An Empty describes the point a character must stand before beginning the "get into car" animation.

Kurt-Dekker and spiney199 like this.