Planet gravity in 3D with CharacterBody3D

Godot Version

<Godot 4.2.2>


<I am making a 3D space exploaration game and am trying to add an outer wilds planet gravity system but im using a CharacterBody3D. Help please!>

	// The direction to the planet.
	Vector3 planetDirection = (planet.GlobalPosition - GlobalPosition).Normalized();
	// Apply the gravity.
	Vector3 velocity = Velocity;
	velocity += planetDirection * gravity;
	Velocity = velocity;

	// Rotate the characterBody so that the feet touch the ground.
	Quaternion newRot = new Quaternion(Vector3.Up, planetDirection);
	GlobalRotation = newRot.GetEuler();

1 Like

My code is gdscript so i need some gdscript code