#Not understanding this code completly

11 messages · Page 1 of 1 (latest)

harsh scaffold
#

So i did this tutorial on youtube with movement and i made this code, but i dont understand it completly. I do understand some of it but i would like someone explaining it all to me. Thanks! The code is gonna be here in the thread

#

extends KinematicBody

var speed = 10
var h_acceleration = 6
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 20
var jump = 10
var full_contact = false

var mouse_sensitivity = 0.03

var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()

onready var head = $Head
onready var ground_check = $GroundCheck

func _ready():
pass

func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))

func _physics_process(delta):

direction = Vector3()

full_contact = ground_check.is_colliding()

if not is_on_floor():
    gravity_vec += Vector3.DOWN * gravity * delta
    h_acceleration = air_acceleration
elif is_on_floor() and full_contact:
    gravity_vec = -get_floor_normal() * gravity
    h_acceleration = normal_acceleration
else:
    gravity_vec = -get_floor_normal()
    h_acceleration = normal_acceleration
    
if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
    gravity_vec = Vector3.UP * jump

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_left"):
    direction -= transform.basis.x
elif Input.is_action_pressed("move_right"):
    direction += transform.basis.x

direction = direction.normalized()
h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
movement.z = h_velocity.z + gravity_vec.z
movement.x = h_velocity.x + gravity_vec.x
movement.y = gravity_vec.y

move_and_slide(movement, Vector3.UP)
crisp crane
#

There's a lot here - could you point out a few specific things that are really confusing to you? Respectfully, if you truly don't understand anything of what's going on here, you may want to go look at some more basic docs/tutorials first. But if it's just a handful of areas that aren't making sense I'd be happy to try and explain.

harsh scaffold
#

Sure

#

For example

#

Starting with

#

If not is on floor

#

Until normal_qcceleration

rugged mural
#
extends KinematicBody

var speed = 10
var h_acceleration = 6
var air_acceleration = 1
var normal_acceleration = 6
var gravity = 20
var jump = 10
var full_contact = false

var mouse_sensitivity = 0.03

var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()

onready var head = $Head
onready var ground_check = $GroundCheck

func _ready():
    pass

func _input(event):
    if event is InputEventMouseMotion:
        rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
        head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
        head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
        
func _physics_process(delta):
    
    direction = Vector3()
    
    full_contact = ground_check.is_colliding()
    
    if not is_on_floor():
        gravity_vec += Vector3.DOWN * gravity * delta
        h_acceleration = air_acceleration
    elif is_on_floor() and full_contact:
        gravity_vec = -get_floor_normal() * gravity
        h_acceleration = normal_acceleration
    else:
        gravity_vec = -get_floor_normal()
        h_acceleration = normal_acceleration
        
    if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
        gravity_vec = Vector3.UP * jump
    
    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_left"):
        direction -= transform.basis.x
    elif Input.is_action_pressed("move_right"):
        direction += transform.basis.x
    
    direction = direction.normalized()
    h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
    movement.z = h_velocity.z + gravity_vec.z
    movement.x = h_velocity.x + gravity_vec.x
    movement.y = gravity_vec.y
    
    move_and_slide(movement, Vector3.UP)

For easier reading

crisp crane
#

Sure, so we are covering three different scenarios with this if...elif...else block.

  1. If the player is in the air, we want to apply acceleration due to gravity to make them fall vertically. We also want their horizontal acceleration to be reduced so that its harder for the player to navigate while in midair.
  2. If the player is on the ground, we want them to be kind of sucked in to the floor. If you don't know what a normal vector is, it's the direction that points directly away from a plane. So since we're using the negative of the floor normal, we are pulling the player toward the floor rather than away. Since we're on the floor the horizontal acceleration when the player gives movement input should be snappier than in the air.
  3. This is another case of being on the floor. Note that we're not multiplying the floor attraction vector by gravity this time. Without knowing what ground_check is (clearly its a raycast but I'd have to see it's position and orientation to really get what it's trying to do) I'm not sure what the difference is between this and #2 (the elif block).

Does that help?

harsh scaffold
#

yea