3D moving around sphere

Godot Version

v4.2.2 stable

Question

what I'm trying to achieve is to have planet always underneath my feet

how to do that? I’m new to Godot.

nor rotating doesn’t work nor the moving as it’s moving x and z axis instead relative to how it’s rotated at:

what I wish to know is how to know relative up / right / forward

in unity it’s transform.up / transform.right / transform.forward but here I can’t find it.

extends CharacterBody3D

@onready var camera3D = $visuals/mixamo_base/Camera3D
@onready var animation_player = $visuals/mixamo_base/AnimationPlayer
@onready var planet = $"../Node3D/Node3D2"

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _physics_process(delta):
	look_at_from_position(position, planet.position, Vector3(planet.position - position).normalized())
	# Add the gravity.
	#if not is_on_floor():
		#velocity.y -= gravity * delta

	# Handle jump.
	#if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		#velocity.y = JUMP_VELOCITY

	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	# need different forward relative not static
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity = direction * SPEED
		if animation_player.current_animation != "walking":
			animation_player.play("walking")
	else:
		if animation_player.current_animation != "idle":
			animation_player.play("idle")
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.y = move_toward(velocity.y, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)
	move_and_slide()

func _input(event):
	if event is InputEventMouseMotion:
		rotate_y(deg_to_rad(-event.relative.x))# wrong as it's only static axis
		#rotate() I need relative y axis not up as sometimes user is up side down
		camera3D.rotate_x(deg_to_rad(event.relative.y))

You can find your answer in this video

1 Like

like I’m not using same godot

there’s full of the stuff missing in the rigidbody

You need to learn the concept from that video then you can yourself create this planet gravity/physics

1 Like

ok I’ve finally figured it out.

solution:

gravityDirection = GameManager.planetPosition - position
	rotation = (Quaternion(-transform.basis.y, gravityDirection) * transform.basis.get_rotation_quaternion()).normalized().get_euler()

this is the script:

extends CharacterBody3D

@onready var body = $body
@onready var camera = $body/Node3D/Camera3D
@onready var characterBody = $"."


const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const sensitivity = 0.01;
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = 0.98
var gravityDirection;

func _unhandled_input(event):
	if event is InputEventMouseMotion:
		body.rotate_y(-event.relative.x * sensitivity)
		camera.rotate_x(-event.relative.y * sensitivity)

func _physics_process(delta):
	gravityDirection = GameManager.planetPosition - position
	rotation = (Quaternion(-transform.basis.y, gravityDirection) * transform.basis.get_rotation_quaternion()).normalized().get_euler()
	
	
	# Add the gravity.
	if not characterBody.is_on_floor():
		characterBody.velocity += gravityDirection * gravity * delta
		print(gravityDirection)

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and characterBody.is_on_floor():
		characterBody.velocity = gravityDirection * JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	
	var direction = (body.global_basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		characterBody.velocity = direction * SPEED
	else:
		characterBody.velocity.x = move_toward(characterBody.velocity.x, 0, SPEED)
		characterBody.velocity.z = move_toward(characterBody.velocity.z, 0, SPEED)
		characterBody.velocity.y = move_toward(characterBody.velocity.y, 0, SPEED)

	characterBody.move_and_slide()

I’m sorry but I couldn’t find the options that were on the video I’m just too noobish in GoDot for now, …

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