Vector3 math converting from GDScript to c#

Godot Version

4.2.1

Question

Hey all ,
Im trying to convert some tutorial from GDscript to c# and having trouble.

I have this code :

var body_to_move : KinematicBody = null

export var move_accel = 4
export var max_speed = 25
var drag = 0.0
export var jump_force = 30
export var gravity = 60

var pressed_jump = false
var move_vec : Vector3
var velocity : Vector3
var snap_vec : Vector3
export var ignore_rotation = false

signal movement_info

var frozen = false

func _ready():
	drag = float(move_accel) / max_speed

func init(_body_to_move: KinematicBody):
	body_to_move = _body_to_move

func jump():
	pressed_jump = true

func set_move_vec(_move_vec: Vector3):
	move_vec = _move_vec.normalized()

func _physics_process(delta):
	if frozen:
		return
	var cur_move_vec = move_vec
	if !ignore_rotation:
		cur_move_vec = cur_move_vec.rotated(Vector3.UP, body_to_move.rotation.y)
	velocity += move_accel * cur_move_vec - velocity * Vector3(drag, 0, drag) + gravity * Vector3.DOWN * delta
	velocity = body_to_move.move_and_slide_with_snap(velocity, snap_vec, Vector3.UP)

and as i know the setting of the vectors are not the same as in c#
so this is what i figure out until:

using Godot;
using System;

public partial class CharacterMover : Node3D
{
	[Export]
	private int MoveAccel = 4;
    
	[Export]
	private int MaxSpeed = 25;

    private float Drag = 0.0f;

	[Export]
	private int JumpForce = 30;

	[Export]
	private int Gravity = 60;

	private bool PressedJump = false;
	private Vector3 MoveVec;
	private Vector3 Velocity;
	private Vector3 SnapVec;
	
	[Export]
	private bool IgnoreRotation = false;

	private CharacterBody3D BodyToMove;
	
	[Signal]
	public delegate void MovementInfoEventHandler(Vector3 velocity,bool grounded);

    private bool frozen = false;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
	{
		Drag = (float)MoveAccel / MaxSpeed;
	}

	public void Init(CharacterBody3D bodytomove)
	{
		BodyToMove = bodytomove;
	}

	public void Jump()
	{
		PressedJump = true;
	}

	public void SetMoveVec(Vector3 movevec)
	{
		MoveVec = movevec.Normalized();
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
    }

    public override void _PhysicsProcess(double delta)
	{
		if (frozen)
		{
			return;
		}

		Vector3 CurMoveVec = MoveVec;

		if (!IgnoreRotation)
		{
			CurMoveVec = CurMoveVec.Rotated(Vector3.Up, BodyToMove.Rotation.Y);
                              // HERE I Dont know how to convert the code to c#
        }

    }
}

So how i convert this :

cur_move_vec = cur_move_vec.rotated(Vector3.UP, body_to_move.rotation.y)
	velocity += move_accel * cur_move_vec - velocity * Vector3(drag, 0, drag) + gravity * Vector3.DOWN * delta
	velocity = body_to_move.move_and_slide_with_snap(velocity, snap_vec, Vector3.UP)

to c# ?
Thanks

I have a partial answer,

public override void _PhysicsProcess(double delta)
{
    if (frozen)
    {
        return;
    }

    Vector3 CurMoveVec = MoveVec;

    if (!IgnoreRotation)
    {
        CurMoveVec = CurMoveVec.Rotated(Vector3.Up, BodyToMove.Rotation.Y);
    }

    Velocity += MoveAccel * CurMoveVec - Velocity * new Vector3(Drag, 0, Drag) + Gravity * Vector3.Down * (float)delta;

    // move_and_slide_with_snap?
}

In C# delta is given as a double, whereas vector types use floats. You usually have to cast the double as a float. This is because float is a smaller datatype, meaning there’s a loss in precision.

In your code, it looks like those second two lines are outside of the if statement, so in C# they should be outside of the curly brackets.

As for the third line, I’m not familiar with “move_and_slide_with_snap” and I couldn’t find it in the CharacterBody3D documentation. It looks like you’re using a different type in your gd script than in your C# code: var body_to_move : KinematicBody = null

1 Like

move_and_slide_with_snap is from godot 3 , im looking for its equivalent in godot 4 c#
have any idea?

I’m new to 3D. In 2D, you would change the Velocity property of the CharacterBody2D and then call MoveAndSlide();

    public override void _PhysicsProcess(double delta)
    {
        var vel = this.Velocity;
        // do calculations
        this.Velocity = vel;
        MoveAndSlide();
    }

CharacterBody3D probably works in a similar way.

1 Like

Thanks it helped

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