Limiting and Rotating a Platform.

I am having some trouble finding the correct code nessesary to rotate a platform by the x, and z-axises using input of the w,a,s,d keys. I have tried several methods, such as using a rigidbody and AddTorque, but I can’t stop it from rotating after a while and all my objects fall off. Perhaps there is a way I can add a max rotation?

And Ideas? Either in Java or C#, I can understand and convert both.

Also, my platform has child objects so I would put static objects in the main platform right?

Thanks 2 tons!

Well you should use Quaternion rotations if you want to control it by axes.

You should have 2 member variables which contain the values for the rotation, example of script attached to platform:

    float _Xrotation = 0.0f;
    float _Zrotation = 0.0f;

    float _MaxXrotation = 15.0f;
    float _MaxZrotation = 15.0f;

    float _RotSpeed = 3.0f;

    Quaternion _StartRotation;

    void Start()
    {
        _StartRotation = this.transform.rotation;
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.W))
        {
            _Xrotation -= Time.deltaTime * _RotSpeed;
            _Xrotation = Mathf.Clamp(_Xrotation, -_MaxXrotation, _MaxXrotation);    //Make sure the value stays within maximum rotation
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            _Xrotation += Time.deltaTime * _RotSpeed;
            _Xrotation = Mathf.Clamp(_Xrotation, -_MaxXrotation, _MaxXrotation);    //Make sure the value stays within maximum rotation
        }

        if (Input.GetKeyDown(KeyCode.A))
        {
            _Zrotation -= Time.deltaTime * _RotSpeed;
            _Zrotation = Mathf.Clamp(_Zrotation, -_MaxZrotation, _MaxZrotation);    //Make sure the value stays within maximum rotation
        }

        if (Input.GetKeyDown(KeyCode.D))
        {
            _Zrotation += Time.deltaTime * _RotSpeed;
            _Zrotation = Mathf.Clamp(_Xrotation, -_MaxZrotation, _MaxZrotation);    //Make sure the value stays within maximum rotation
        }

        this.transform.rotation = _StartRotation * Quaternion.AngleAxis(_Xrotation, Vector3.forward) * Quaternion.AngleAxis(_Zrotation, Vector3.left);
    }

The rotation of your plane is constantly calculated, based on its start rotation, multiplied with each custom axis rotation, which you can control perfectly.

I probably mixed up keys and axes in this example, but it should give you a basic idea.

Hope this helps :wink: