Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | abbos |
Hi, a friend sent me this code to move in 3D and to rotate the camera by shaking the mouse, but the camera only moves in the y-axis and the character does not move
var speed var gravity = 0 var walk = 10 # walk speed var sprint = 20 # sprint speed var jump = 15 # jump height var fall = 0.7 # fall speed var xsens = 0.08 # mouse x sensitivity var ysens = 0.07 # mouse y sensitivity func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) func _input(event): if event is InputEventMouseMotion: var m = event.relative var r = clamp($Camera.rotation_degrees.x - m.y * ysens,-70,70) $Camera.rotation_degrees.x = r rotation_degrees.y -= m.x * xsens func _physics_process(delta): var dir = Vector3() if Input.is_key_pressed(KEY_SHIFT): speed = sprint else: speed = walk if is_on_floor(): if Input.is_key_pressed(KEY_SPACE): gravity = jump else: gravity = 0 else: gravity -= fall dir.x = int(Input.is_key_pressed(KEY_D)) dir.x -= int(Input.is_key_pressed(KEY_A)) dir.z += int(Input.is_key_pressed(KEY_S)) dir.z -= int(Input.is_key_pressed(KEY_W)) dir = global_transform.basis.xform(dir.normalized()*speed) dir.y = gravity move_and_slide(dir,Vector3.UP)
Hi! It’s really difficult to debug someone else’s code as is, but especially if it isn’t formatted as the actual code would be. For starters, could you please try and re-upload the code using the “sample code” button above your text box (as shown by the curly braces icon)?
This will help insure you get the best answers possible.
Thanks!
maxwellc055 | 2020-09-11 17:08
Hi! would you mind to convert this to a comment? As people may wrongly interpret this question is already answered.
p7f | 2020-09-11 18:49
+1 (this should be a comment)
jgodfrey | 2020-09-11 19:08
Ah yes! Of course. Thanks
maxwellc055 | 2020-09-11 19:35