I'm making this game with space travel so the space you walk on is round like a planet. Once you get into the atmosphere a signal is emitted letting the playerscript know that he's on a planet and what planet it is. I think I've got the gravity and the camera movement down but i really need the player to face the direction he's going. I don't know how to explain it very well but here are the scripts involved. Please help. The fact that the gravity vector changes depending on where on the planet you are in confusing me when figuring out which way is up for the rotation of the player.

extends KinematicBody

var currentplanet
var gravity
var gravity
dir
var NoGravity
var velocity = Vector3()
var gravitystrength = 0
var dist
to_plan

var speed = 1

var cammovespeed = 0.005
var roty = 0
var rot
x = 0

var camera
var moving
var landing

const ACCELERATION = 4
const DECELERACTION = 6

Input.set
mousemode(Input.MOUSEMODE_CAPTURED)
pass

func physicsprocess(delta):
getinput(delta)
camera = get
node("Spatial0/Spatial/Spatial2/Camera").getglobaltransform()

``````if current_planet != null:
### gravity direction ###
var planet_pos = current_planet.global_transform.origin
var player_pos = global_transform.origin
gravity_dir = (planet_pos - player_pos)
var up = gravity_dir

### gravity strength ###
dist_to_plan = gravity_dir.length()
gravity_strength = 1 - dist_to_plan/100

### keeps player upright ###
transform = transform.orthonormalized()
if transform.basis.y.normalized().cross(gravity_dir) != Vector3():
look_at(current_planet.global_transform.origin, transform.basis.y)
elif transform.basis.x.normalized().cross(gravity_dir) != Vector3():
look_at(current_planet.global_transform.origin, transform.basis.x)

move_and_slide(gravity_dir*gravity_strength, up)
else:
NoGravity = true

velocity = move_and_slide(velocity, Vector3(0,1,0))
``````

func newplanet(planet):
current
planet = planet

func getinput(delta):
moving = false
var dir = Vector3()
if Input.is
actionpressed("Forward"):
dir += -camera.basis
moving = true
if Input.is
actionpressed("Back"):
dir += camera.basis
moving = true
if Input.is
actionpressed("Left"):
dir += -camera.basis
moving = true
if Input.is
actionpressed("Right"):
dir += camera.basis
moving = true
if Input.is
actionpressed("Up"):
dir += camera.basis
moving = true
if Input.is
action_pressed("Down"):
dir += -camera.basis
moving = true

``````if Input.is_action_pressed("ui_accept"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

dir = dir.normalized()

var hv = velocity

var new_pos = dir * speed
var accel = DECELERACTION
if (dir.dot(hv) > 0):
accel = ACCELERATION
hv = hv.linear_interpolate(new_pos, accel * delta)
velocity.y = hv.y
velocity.x = hv.x
velocity.z = hv.z
``````

-----AND THE CAMERA MOVEMENT SCRIPT-----

extends Spatial

var rotx = 0
var rot
y = 0
var distance = 0.1

export var cammovespeed = 0.005

pass

func physicsprocess(delta):
var camera
pos = getnode("Spatial/Spatial2/Camera").globaltransform.origin

``````if Input.is_action_pressed("ZoomIn"):
distance -= .05
if Input.is_action_pressed("ZoomOut"):
distance += .05

distance = clamp(distance, 0.5, 2)
var offset = camera_pos - global_transform.origin
offset = offset.normalized() * distance
get_node("Spatial/Spatial2/Camera").global_transform.origin = global_transform.origin + offset
``````

func unhandledinput(event):
if event is InputEventMouseMotion:
rotx += event.relative.x * cammovespeed
rot
y += event.relative.y * cammovespeed
transform.basis = Basis() # reset rotation
rotateobjectlocal(Vector3(0,0,-1), rotx)
rotate
objectlocal(Vector3(0,-1,0), roty)
if roty >= 1.5:
rot
y = 1.499
if roty <= -1.5:
rot
y = -1.499

Note: I have been using godot for a while now and i just registered today so that I could ask this question. It's driving me nuts. I love godot. Please help.

• Clean up the post by placing all code in proper `code` tags (the `{}` button in the post editor toolbar).