Hey everyone! Thank you for clicking on my question. This is my first post on this website, so feel free to leave some constructive criticism regarding my post, if you'd like.
I'm relatively new to Godot and I have recently started my first 3D project. I'm making a shooter, where the player is defending an objective against waves of enemies. This takes place on a planet with "realistic" gravity. I watched and read plenty of tutorials, but I could not find answers to my question.
This is the problem I ran into: I want to apply current_gravity to the player in a way that it always pulls the player toward the center of the planet, but I don't know how to do it.
Here is a short video of my project so far: https://youtu.be/yJkafqUAZnU
var speed = 20
var acceleration = 20
var earth_gravity = 9.8
var current_gravity = earth_gravity
var MAX_SLOPE_ANGLE = 40
var fall = Vector3()
var jump_power = 200
var jump_pressed = 0
var mouse_sensitivity = 0.05
var is_on_ground = false
onready var Head = $Head
This rotates the player or the player's head when the mouse is moved.
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
Head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
#Declares a variable that serves as the up vector
var up = get_gravity_dir_vector()
This checks if player is standing on ground.
is_on_ground = true
is_on_ground = false
This rotates the player toward the center of the planet. I copied it from one of Pigeonaut's videos on Youtube.
transform = transform.orthonormalized()
if transform.basis.y.normalized().cross(get_gravity_dir_vector()) != Vector3():
elif transform.basis.x.normalized().cross(get_gravity_dir_vector()) != Vector3():
This part should apply gravity to the player, when they are not standing on ground.
if not is_on_ground:
# fall.y += current_gravity * delta <== This works on a flat surface, but not on a sphere. I tried rotating fall, but it didn't work.
This part should make the player jump.
if Input.is_action_pressed("jump") and is_on_ground:
# fall.y = jump_power <== This also works on a flat surface, but not on the planet.
This handles player movement on the surface of the planet.
var vel = Vector3()
vel += transform.basis.y
vel -= transform.basis.y
vel -= transform.basis.x
vel += transform.basis.x
vel *= speed
vel = move_and_slide(vel,get_gravity_dir_vector(),0.05,4,deg2rad(MAX_SLOPE_ANGLE), false)
This part is supposed to move the player vertically.
This calculates the up direction relative to the player's position on the planet.
return(Global.world_node.get_node("Planet").transform.origin - transform.origin).normalized()