Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Blobb2030 |
note: this is my first project
I was copying this tutorial on character movement (https://www.youtube.com/watch?v=UpF7wm0186Q) and once I had written it when I started the game gravity didn’t work and I couldn’t move the player. (The camera is working fine)
Here is my code
Player.gd
extends KinematicBody
export var speed := 7.0
export var jump_strength := 20.0
export var gravity := 50.0
var _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN
onready var _spring_arm: SpringArm = $SpringArm
onready var _model: Spatial = $CSGMesh
func _physics_process(delta: float) -> void:
var move_direction := Vector3.ZERO
move_direction.x = Input.get_action_raw_strength("right") - Input.get_action_strength("left")
move_direction.z = Input.get_action_raw_strength("back") - Input.get_action_strength("forward")
move_direction = move_direction.rotated(Vector3.UP, _spring_arm.rotation.y).normalized()
_velocity.x = move_direction.x * speed
_velocity.z = move_direction.z * speed
_velocity.y -= gravity * delta
var just_landed := is_on_floor() and _snap_vector == Vector3.ZERO
var is_jumping := is_on_floor() and Input.is_action_just_pressed("jump")
if is_jumping:
_velocity.y = jump_strength
_snap_vector = Vector3.ZERO
elif just_landed:
_snap_vector = Vector3.DOWN
_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
if _velocity.length() > 0.2:
var look_direction = Vector2(_velocity.z, _velocity.x)
_model.rotation.y = look_direction.angle()
func _process(_delta: float) -> void:
_spring_arm.translation = translation
spring_arm.gd
extends SpringArm
export var mouse_sensitivity := 0.05
func _ready() -> void:
set_as_toplevel(true)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
rotation_degrees.x -= event.relative.y * mouse_sensitivity
rotation_degrees.x = clamp(rotation_degrees.x, -90.0, 30.0)
rotation_degrees.y -= event.relative.x * mouse_sensitivity
rotation_degrees.y = wrapf(rotation_degrees.y, 0.0, 360.0)