0 votes

Hi all

I am struggling to use moveandslide correctly - I am pretty sure I am missing an elephant here! I have a 3D scene with a kinematic body which should move to a predetermined location up on a key press.

I have the scene arranged fairly standard as below

 - camera
 - lights

 - player[kinematic body]
 - floor [static body]

I will show the code below. Please ignore the camera rotation code in it. They way I coded below works where upon pressing A, it moves to where it should go - fairly ok.

cloc:(0, 0, 0) diff:(0, 0, 3)
cloc:(0, 0, 0.1) diff:(0, 0, 2.9)
cloc:(0, 0, 0.213333) diff:(0, 0, 2.786667)
cloc:(0, 0, 0.341778) diff:(0, 0, 2.658222)
cloc:(0, 0, 3.066901) diff:(3, 0, -3.066901)

However if I press D, it goes berserk and moves way out and in the wrong direction.

cloc:(0.213333, 0, 3.939264) diff:(2.786667, 0, -3.939264)
cloc:(0.341778, 0, 4.464499) diff:(2.658222, 0, -4.464499)
cloc:(2.617854, 0, 13.771832) diff:(0.382146, 0, -13.771832)

Keys W and S don't even move the kinematic body.

Code is below but what am I missing?
Thanks everybody

extends Spatial

var loc1=Vector3(-3,0,0)
var loc2=Vector3(3,0,0)
var loc3=Vector3(0,0,3)
var loc4=Vector3(0,0,-3)
var upnorm=Vector3(0,1,0)
var speed=10
var target=Vector3()

var mouse_sens = 0.3
var camera_anglev=0

func _ready():

func _physics_process(delta):
    var cloc=$ply.global_transform.origin
    var diff=target-cloc
    if diff.x>0.1 or diff.z>0.1:
        print("cloc:",cloc," diff:",diff)
        var vel=cloc.linear_interpolate(target,0.2)

func _input(event):
    if Input.is_key_pressed(KEY_W):
    if Input.is_key_pressed(KEY_A):
    if Input.is_key_pressed(KEY_S):
    if Input.is_key_pressed(KEY_D):
    if Input.is_key_pressed(KEY_X):
in Engine by (91 points)

1 Answer

0 votes

The problem with W and S is simple. Your diff is always smaller than 0.1 for these as it is a negative value (-3 - 0.9 = - 3.9 < 0.1). abs(diff) should fix this.

D is a little bit more tricky. I have no idea why it would behave like this...

by (330 points)

Hi there

I figured the W and S. Thanks for pointing it out too. I am still stumped by the D.

Also I figured that since I don't want the collisions to happen for a remote players local representations, I used the ply.global_transform.origin and set it with linear interpolation and it works.

It will be nice to know how to use it though! If I find a way - I will add it here. Hopefully someone who knows may answer.


Found the problem. It is not "D" but the fact that it's the second target to move to. For move_and_slide you need a movement vector pointing in the target direction. linear_interpolate doesn't give you this but instead a point laying in between your target and your current position. This does work fine as long as you are moving in a straight line from 0,0,0 but fails as soon as you don't. (AKA the second target you move to)
You have to either subtract your current position from the result of linear_interpolate or use another method altogether.

I hope this helps.

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.