On gdscript, it is very easy obtain height:
CollisionShape.shape.height
But I don't have any idea how to get the height of collision shape on C#. On C# it is missing the reference "Height"
CollisionShape.Shape.??
On gdscript, it is very easy obtain height:
CollisionShape.shape.height
But I don't have any idea how to get the height of collision shape on C#. On C# it is missing the reference "Height"
CollisionShape.Shape.??
GDScript:
$CollisionShape
Is equivalent to
get_node("CollisionShape")
Which translates to C# as:
GetNode("CollisionShape")
However, C# cares for types, so do this:
GetNode<CollisionShape>("CollisionShape")
Now it should show you the Shape property:
GetNode<CollisionShape>("CollisionShape").Shape
Ern... Not all Shape have "height". C# won't let you use it, until it knows it is a type that has it. I'll assume it is a capsule shape, so do this:
var shape = (CapsuleShape)GetNode<CollisionShape>("CollisionShape").Shape;
If you are not sure it is a CapsuleShape, you can do something like this:
if (GetNode<CollisionShape>("CollisionShape").Shape is CapsuleShape shape)
{
// ...
}
Or like this (which could give you null, if it isn't):
Shape? shape = GetNode<CollisionShape>("CollisionShape").Shape as CapsuleShape;
Now you should be able to get Height:
var height = shape.Height;
No, the extra variables not always necessary.
If you don't want to use types, you have an alternative -less safe, less performant- way of doing things:
var height = (float)GetNode("CollisionShape").Get("Shape").Get("Height");
Sometimes it is convenient to write things like this to begin with, and convert to the other way as necessary. In particular while translating code from GDScript to C#.
It is also possible to do a mix of the two approaches:
var height = (float)GetNode<CollisionShape>("CollisionShape").Shape.Get("Height");
I believe this can also be done in C# using dynamic, here:
var height = ((dynamic)GetNode("CollisionShape").Shape).Height;
Or here:
var height = ((dynamic)GetNode("CollisionShape")).Shape.Height;
However, you are not going to get IntelliSense.