What is the equivalent of gdscriptcamera.rotation.x to csharp

Godot Version 4.2 mono

Question

Hello all
I try to convert this simple code to csharp ,

camera.rotation.x = 6

but while i look at the csharp inner code in Node3D :

public Vector3 Rotation
        {
            get
            {
                return GetRotation();
            }
            set
            {
                SetRotation(value);
            }
        }

and this from Vector3d:

public float this[int index]
        {
            readonly get
            {
                return index switch
                {
                    0 => X,
                    1 => Y,
                    2 => Z,
                    _ => throw new ArgumentOutOfRangeException("index"),
                };
            }
            set
            {
                switch (index)
                {
                    case 0:
                        X = value;
                        break;
                    case 1:
                        Y = value;
                        break;
                    case 2:
                        Z = value;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException("index");
                }
            }
        }

in my code i can’t get any access to the rotation x
This not working:

camera3D.Rotation[Vector3.Axis.X] = 6 ; 
or 
camera3D.Rotation[0] = 6 ;

both gives me :
first gives error:
|Error|CS1503|Argument 1: cannot convert from 'Godot.Vector3.Axis' to 'int'
the second :
|Error|CS1612|Cannot modify the return value of 'Node3D.Rotation' because it is not a variable

First error:
Vector3.Axis.X is an enum, you have to cast it explicitly to an int by stating (int)Vector3.Axis.X.

Second error:
Since Node3D.Rotation is a property of type Vector3 which is produced as the result of an intermediate expression but is not stored in a variable, you have to set the entire property.

// CS1612 error
camera3D.Rotation[0] = 6;
camera3D.Rotation.X = 6;

// This will work
camera3D.Rotation = new Vector3(6, camera3D.Rotation.Y, camera3D.Rotation.Z);
2 Likes

Working with/around CS1612 is explained in the documentation: C# basics — Godot Engine (stable) documentation in English

1 Like

thank you all but i most ask if creating new verctor3d each time if i doing like this:

public override void _UnhandledInput(InputEvent @event)
    {
        //base._UnhandledInput(@event);
		if (@event is InputEventMouseMotion mouseEvent) {
			head.RotateY(-mouseEvent.Relative.X * SENSITIVITY);
			head.RotateX(-mouseEvent.Relative.Y * SENSITIVITY);
			float cameraRotationX = Mathf.Clamp(camera3D.Rotation.X,Mathf.DegToRad(-40),Mathf.DegToRad(60));
			camera3D.Rotation = new Vector3(cameraRotationX, camera3D.Rotation.Y, camera3D.Rotation.Z);
			

        } 
    }

is it not waste of memory ?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.