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
testing[spatial]
- camera
- lights
- player[kinematic body]
-shape
-mesh
- floor [static body]
-shape
-mesh
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():
pass
func _physics_process(delta):
var cloc=$ply.global_transform.origin
cloc.y=0
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)
$ply.move_and_slide(vel*speed,upnorm)
func _input(event):
if Input.is_key_pressed(KEY_W):
target=loc4
if Input.is_key_pressed(KEY_A):
target=loc3
if Input.is_key_pressed(KEY_S):
target=loc1
if Input.is_key_pressed(KEY_D):
target=loc2
if Input.is_key_pressed(KEY_X):
target=Vector3(0,1,0)