0 votes

Hey guys. I'm currently trying to implement collision in my app, i.e. to prevent my player from going through the walls.
The code for the player & camera movements are taken from a tutorial on a website somewhere. I played around with StaticBody and CollisionShape for the walls.
For the player, i used KinematicBody3D. The problem is when I add the CollisionShape to the KinematicBody3D. The collision does behave correctly (i.e. my player stops and slides along the wall when they collide), but the player automatically moves following my mouse direction (ignoring my keboard input).
How can I fix this?

Here is my node tree
Node Tree
My script for the player (KinematicBody3D)

extends KinematicBody

var moveSpeed : float = 20.0
var vel : Vector3 = Vector3()

func _physics_process(delta):

    vel.x = 0
    vel.z = 0

    var input = Vector3()

    if Input.is_action_pressed("ui_up"):
        input.z += 1
    if Input.is_action_pressed("ui_down"):
        input.z -= 1
    if Input.is_action_pressed("ui_left"):
        input.x += 1
    if Input.is_action_pressed("ui_right"):
        input.x -= 1
    if Input.is_action_just_released("ui_up"):
        up_is_pressed = false
    if Input.is_action_just_released("ui_down"):
        down_is_pressed = false
    if Input.is_action_just_released("ui_left"):
        left_is_pressed = false
    if Input.is_action_just_released("ui_right"):
        right_is_pressed = false

    input = input.normalized()

    var dir = (transform.basis.z * input.z + transform.basis.x * input.x)

    vel.x = dir.x * moveSpeed
    vel.z = dir.z * moveSpeed

    vel = move_and_slide(vel, Vector3.UP)

My code for the Cylinder's rotation

extends CSGCylinder

var lookSensitivity : float = 12.0
var minLookAngle : float = -20.0
var maxLookAngle : float = 75.0
var mouseDelta : Vector2 = Vector2()
onready var player = get_parent()

func _ready():


func _input(event):

    if event is InputEventMouseMotion:
        mouseDelta = event.relative

func _process(delta):

    var rot = Vector3(mouseDelta.y, mouseDelta.x, 0) * lookSensitivity * delta

    rotation_degrees.x -= rot.x
    rotation_degrees.x = clamp(rotation_degrees.x, minLookAngle, maxLookAngle)

    player.rotation_degrees.y -= rot.y

    mouseDelta = Vector2()
Godot version 3.4.4
in Engine by (19 points)

1 Answer

0 votes
Best answer

OKAY I've found the answer myself.
Nothing wrong with the codes. The problem is apparently due to me using Cylinder as the CGSMesh shape for the player. I tried using capsule and the problem's gone!

by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.