Hi guys,
I'm attempting to port over what I have in Unity3D to Godot 3.0, and so far it's been fairly simple, but I am stuck on some basic movement code and I can't quite find the commands to work it out.
Basically, I want it so that my character responds to both the joystick input and as well as keyboard and mouse input. I think the solution is I'm just going to have to check to see if the user has a joystick plugged in at startup, if they do I'll use it, otherwise default to mouse+keyboard, but it would be nice if someone could tell me how to sett up an input axis that uses both control schemes!
The other thing is that I want my player to rotate depending on input provided, so if you start to move down the character will rotate and face down. Same with all other directions. So far, the only directions that work are left/right, and I'm not sure why that would be the case.
I have a video showing what I mean about rotation and positioning:
And here is my code as it is currently:
extends KinematicBody
export var Gravity = -20
export var GravityMultiplier = 1
export var MoveSpeed = 20
export var TurnSpeed = 20
export var JumpHeight = 10
export var FlightHeight = 10
export var FlightSpeed = 10
var thisGravity = 0
var velocity = Vector3()
var direction = Vector3(0, 0, 0)
func _ready():
set_physics_process(true)
func _physics_process(delta):
_apply_gravity(delta)
_jump()
_move_player()
func _move_player():
# store joystick input
var input_axis = Vector2(Input.get_joy_axis(0, JOY_AXIS_0), Input.get_joy_axis(0, JOY_AXIS_1))
var h = input_axis.x
var v = input_axis.y
# set the player's rotation to the values as given by the joystick
set_rotation(Vector3(0, input_axis.angle(), 0))
direction = Vector3(h, 0, v)
# var forward = Vector3()
# add the input values to the velocity
velocity.x = h * MoveSpeed
velocity.y = thisGravity
velocity.z = v * MoveSpeed
# move the player
velocity = move_and_slide(velocity, Vector3(0, 1, 0))
func _apply_gravity(delta):
if is_on_floor():
thisGravity = 0
else:
thisGravity += Gravity * GravityMultiplier * delta
func _jump():
if is_on_floor() and Input.is_action_pressed("ui_select"):
thisGravity += JumpHeight
And lastly, relating to the above paragraph, how would one determine an object's Forward Vector in GD script?
For all of this, I would like to use the KinematicBody for my character as it seems by far the easiest method to work things out. However, if someone has another suggestion or knows of a way to get what I want done with rigidbodies, don't be afraid to tell me to change haha!
Thanks guys, I'm loving Godot 3.0 so far.