'How to freeze rotation in the z & y axes directions? Unity3D

I have a platform that can swing on a wide cylinder if objects are unevenly distributed on it.

To do this, I created a cylinder with a mesh collider, put a platform on top of it, whose centers coincide with each other.

Then I attached a code to the platform that freezes its position, that is, the platform can now only rotate, but not fall.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DeterminantOfPlatformEquilibrium : MonoBehaviour
{
   Vector3 newYPos;

    void Start()
    {
    newYPos = transform.position;
    }

    void Update()
    {
        /*We're setting the position of the object in the
        "y" direction - unchanged, and equal to zero, 
        so that the platform does not fall becaude of gravity*/
newYPos.y += 0.0f;
transform.position = newYPos;
newYPos += new Vector3(0f, 0f, 0f);

    }
}

In addition to freezing the position of the platform, I must also freeze its rotations in the y & z directions, so that it rotates in only one direction - in x.

I tried to do this using the call to the Euler () method from the Quaternion class, set a certain angle of rotation to the x axis, and set the other 2 axes to zero, and it seemed to me that everything should work, but this line does not seem to do anything in my code: Quaternion rotation = Quaternion.Euler(23f, 0f, 0f);

Do you have more possible options for freezing these 2 axes?



Solution 1:[1]

I see you are new to Unity3D. Your "Position freezing" actually does nothing, you are just adding zero to the position, and adding zero to something doesn't change it. If your cylinder doesn't have Rigidbody component it won't fall because of gravity.

Your rotating doesn't do anything to you, because you make a variable called rotation but you don't assign it anywhere. Also, instead of writing zeros for Y and Z you can multiply Vector3.right by your X value. So the code to rotate your cylinder in X direction would be:

transform.rotation = Quaternion.Euler(Vector3.right * 23f);

Solution 2:[2]

I am not sure if I didn't misunderstand your question, but this line of code:

Quaternion rotation = Quaternion.Euler(23f, 0f, 0f);

doesn't do anything, because you never use the variable you have created. If you wanna set the rotation try:

transform.rotation = Quaternion.Euler(...)

If you are using rigidbody you can also constrain rotation around specific axis in inspector.

Solution 3:[3]

Freeze All Rotation

    rigidbody.constraints = RigidbodyConstraints.FreezeRotation;

Freeze Specific Rotation:

    rigidbody.constraints = RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;

You can find view Unity Documentation , but they did not explained it well

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Drifts
Solution 2 Tomáš Br?na
Solution 3 Single-byte