I have been following Garbaj's third person controller tutorial, but it didn't include jumping, so I'm taking pieces of his first person controller tutorial to add gravity and jumping. I've come up with this script as a result
extends KinematicBody
var speed = 8
var acceleration = 5
var mouse_sensetivity = 0.1
var gravity = 20
var jump_speed = 10
var full_contact = false
var direction = Vector3()
var velocity = Vector3()
var gravity_vec = Vector3()
var movement = Vector3()
onready var pivot = $Pivot
onready var ground_check = $GroundCheck
func _input(event):
if Input.is_action_just_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensetivity))
pivot.rotate_x(deg2rad(-event.relative.y * mouse_sensetivity))
pivot.rotation.x = clamp(pivot.rotation.x, deg2rad(-89), deg2rad(89))
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
print("testing")
func physics_process(delta):
direction = Vector3()
if ground_check.is_colliding():
full_contact = true
print("full_contact")
else:
full_contact = false
print("not colliding")
if not is_on_floor():
gravity_vec += Vector3.DOWN * gravity * delta
elif is_on_floor() and full_contact:
gravity_vec = -get_floor_normal() * gravity
else:
gravity_vec = -get_floor_normal()
if Input.is_action_just_pressed("jump") and (is_on_floor() or full_contact):
gravity_vec = Vector3.UP * jump_speed
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
elif Input.is_action_pressed("move_backward"):
direction += transform.basis.z
if Input.is_action_pressed("move_right"):
direction += transform.basis.x
elif Input.is_action_pressed("move_left"):
direction -= transform.basis.x
direction = direction.normalized()
velocity = direction * speed
velocity = velocity.linear_interpolate(velocity, acceleration * delta)
movement.x = velocity.x + gravity_vec.x
movement.y = gravity_vec.y
movement.z = velocity.z + gravity_vec.z
move_and_slide(movement, Vector3.UP)
everything worked fine, till I changed this section: (the above script includes the broken script, I am showing you the original block of code now.):
the original block of code was this:
if not is_on_floor():
gravity_vec += Vector3.DOWN * gravity * delta
else:
gravity_vec = -get_floor_normal() * gravity
if Input.is_action_just_pressed("jump"):
gravity_vec = Vector3.UP * jump_speed
and I replaced it with this:
if ground_check.is_colliding():
full_contact = true
print("full_contact")
else:
full_contact = false
print("not colliding")
if not is_on_floor():
gravity_vec += Vector3.DOWN * gravity * delta
elif is_on_floor() and full_contact:
gravity_vec = -get_floor_normal() * gravity
else:
gravity_vec = -get_floor_normal()
if Input.is_action_just_pressed("jump") and (is_on_floor() or full_contact):
gravity_vec = Vector3.UP * jump_speed
(This is all part of the fps tutorial by Garbaj, I'm trying to show you where I went wrong.)
Now my character can't move! Also, the print functions I put up for ground check aren't showing in the console, however my testing print function in ready is! so for some reason the groundcheck raycast (which is on the bottom of the character by the way ) is not colliding! I've checked and it is enabled. Did i do something wrong with my code?
Sorry for the long code, but I'm not sure how else to write it. Thanks to anyone who looks at or answers this. =)
`