john lohmann
john lohmann

Reputation: 11

Godot player not walking in the direction

So I am new to godot and I was having a problem. The players body doesnt follow the camera when the camera moves When I look around with the camera the players body stays the same e.g. if i use wasd if i turn to the right it would be aswd. the player movement is hard but i got help. if you could help that will be super cool, i am just trying to learn coding. this is hard for me.

p.s sorry for the bad grammar

extends KinematicBody

signal hit

# How fast the player moves in meters per second.
export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
export var fall_acceleration = 50
# Vertical impulse applied to the character upon jumping in meters per second.
export var jump_impulse = 30
# Vertical impulse applied to the character upon bouncing over a mob in meters per second.
export var bounce_impulse = 16

# stats
var curHP : int = 10
var maxHP : int = 10
var ammo : int = 15
var score : int = 0


# cam look
var minLookAngle : float = -90.0
var maxLookAngle : float = 90.0
var lookSensitivity : float = 10.0

# vectors
var vel : Vector3 = Vector3()
var mouseDelta : Vector2 = Vector2()

# components
onready var camera : Camera = get_node("Camera")
onready var muzzle : Spatial = get_node("Camera/Muzzle")


# Emitted when a mob hit the player.


var velocity = Vector3.ZERO


func _physics_process(delta):
var direction = Vector3.ZERO

if Input.is_action_pressed("move_right"):
    direction.x += 1
if Input.is_action_pressed("move_left"):
    direction.x -= 1
if Input.is_action_pressed("move_back"):
    direction.z += 1
if Input.is_action_pressed("move_forward"):
    direction.z -= 1
    
#sprinting
if Input.is_action_pressed("move_sprint"):
    speed = 50
if Input.is_action_just_released("move_sprint"):
    speed = 14



velocity.x = direction.x * speed
velocity.z = direction.z * speed

# Jumping.
if is_on_floor() and Input.is_action_just_pressed("move_jump"):
    velocity.y += jump_impulse

velocity.y -= fall_acceleration * delta
velocity = move_and_slide(velocity, Vector3.UP)



func _ready():

# hide and lock the mouse cursor
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 


    
func _process(delta):

# rotate the camera along the x axis
camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta

# clamp camera x rotation axis
camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)

# rotate the player along their y-axis
rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta

# reset the mouseDelta vector
mouseDelta = Vector2()

func _input(event):

if event is InputEventMouseMotion:
    mouseDelta = event.relative

Upvotes: 1

Views: 1596

Answers (1)

Theraot
Theraot

Reputation: 40170

If you want the movement to be based on the orientation of camera...

Then base the movement:

var direction = Vector3.ZERO

if Input.is_action_pressed("move_right"):
    direction.x += 1
if Input.is_action_pressed("move_left"):
    direction.x -= 1
if Input.is_action_pressed("move_back"):
    direction.z += 1
if Input.is_action_pressed("move_forward"):
    direction.z -= 1

On the orientation of the camera.

We will use camera.global_transform.basis. The basis of a transform gives us a set of vectors that are aligned to the axis of the transformed space.

Using camera.transform instead of camera.global_transform will NOT work, because the Camera is a child of the KinematicBody.

Then your code ends up like this:

var direction = Vector3.ZERO
var camera_x = camera.global_transform.basis.x
var camera_z = camera.global_transform.basis.z

if Input.is_action_pressed("move_right"):
    direction += camera_x
if Input.is_action_pressed("move_left"):
    direction -= camera_x
if Input.is_action_pressed("move_back"):
    direction += camera_z
if Input.is_action_pressed("move_forward"):
    direction -= camera_z

Complete script on OP request:

extends KinematicBody

#signal hit

# How fast the player moves in meters per second.
export var speed = 14
# The downward acceleration when in the air, in meters per second squared.
export var fall_acceleration = 50
# Vertical impulse applied to the character upon jumping in meters per second.
export var jump_impulse = 30
# Vertical impulse applied to the character upon bouncing over a mob in meters per second.
# export var bounce_impulse = 16

# stats
# var curHP : int = 10
# var maxHP : int = 10
# var ammo : int = 15
# var score : int = 0


# cam look
var minLookAngle : float = -90.0
var maxLookAngle : float = 90.0
var lookSensitivity : float = 10.0

# vectors
# var vel : Vector3 = Vector3()
var mouseDelta : Vector2 = Vector2()

# components
onready var camera : Camera = get_node("Camera")
# onready var muzzle : Spatial = get_node("Camera/Muzzle")


# Emitted when a mob hit the player.


var velocity = Vector3.ZERO


func _physics_process(delta):
    var direction = Vector3.ZERO
    var camera_x = camera.global_transform.basis.x
    var camera_z = camera.global_transform.basis.z

    if Input.is_action_pressed("move_right"):
        direction += camera_x
    if Input.is_action_pressed("move_left"):
        direction -= camera_x
    if Input.is_action_pressed("move_back"):
        direction += camera_z
    if Input.is_action_pressed("move_forward"):
        direction -= camera_z
        
    #sprinting
    if Input.is_action_pressed("move_sprint"):
        speed = 50
    if Input.is_action_just_released("move_sprint"):
        speed = 14



    velocity.x = direction.x * speed
    velocity.z = direction.z * speed

    # Jumping.
    if is_on_floor() and Input.is_action_just_pressed("move_jump"):
        velocity.y += jump_impulse

    velocity.y -= fall_acceleration * delta
    velocity = move_and_slide(velocity, Vector3.UP)



func _ready():

    # hide and lock the mouse cursor
    Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 


    
func _process(delta):

    # rotate the camera along the x axis
    camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta

    # clamp camera x rotation axis
    camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)

    # rotate the player along their y-axis
    rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta

    # reset the mouseDelta vector
    mouseDelta = Vector2()

func _input(event):

    if event is InputEventMouseMotion:
        mouseDelta = event.relative

Upvotes: 1

Related Questions