I don't necessarily know what I'm doing wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Khan

I picked up Godot about a week or so back and have been slowly and incrementally learning more and more about it. I’ve kind of hit a bit of a stone wall though, in that I can’t seem to get inputs to work. I’ve watched or read now about half a dozen different guides online, and, thus far, no results. I’ve never done game development at all before this, except some small simple GDevelop projects for school so I don’t necessarily know what I’m even getting wrong? I followed all the tutorials (to the letter) with all their Input.is_action_pressed commands, and UI setups, and how to do that. But, when I go to actually play my scene, it doesn’t work.

Any advice would be appreciated. I imagine I’m probably just missing something really obvious.

No one can really tell you what you’re doing wrong if you don’t show your code. Please post your input code.

kidscancode | 2020-11-20 16:31

Well it’s realistically about what you’d expect.

export var speed = 4

func _process(delta):
var velocity = Vector2()
if Input.is_action_pressed(“ui_a”):
velocity.x += 1
if Input.is_action_pressed(“ui_d”):
velocity.x -= 1
if Input.is_action_pressed(“ui_forward”):
velocity.y += 1
if Input.is_action_pressed(“ui_back”):
velocity.y -+ 1
if velocity.length() > 0:
velocity = velocity.normalized() * speed

This is what I got going on trying to get the input to work. All the input keys are mapped in the Project Settings, so I’m not entirely certain what I’ve done wrong. Unless there was a step I missed.

Khan | 2020-11-20 16:51

:bust_in_silhouette: Reply From: kidscancode

Your inputs are setting a variable called velocity, but you’re not actually moving anything. The step you’re missing is to actually use that velocity to move the node. How you move it depends on what type of node it is - since you left off the export line in your copy-and-paste, I can’t tell you anything more specific.

Ohhhhh. Okay. So the variable is being written but not to the object. Allllllright, that would explain it. It’s a KinematicBody2d so I’ll go ahead and see if I can mess around and figure out what I was doing wrong.

Thank you, I appreciate the input!

Khan | 2020-11-20 18:00

Kinematic bodies are moved using their move_and_collide() or move_and_slide() methods. You can find out all about how they work here:
Using KinematicBody2D — Godot Engine (3.2) documentation in English

kidscancode | 2020-11-20 18:35

basically, you can finish your code by adding this line at the bottom:

move_and_slide(velocity, Vector2.UP)

P.S to format code like I just did, select it and press the {} button, this will come in handy because it makes the code a lot easier to read. good luck with your future game development, and keep at it! =)

Millard | 2020-11-20 19:07

That’s assuming slide response is what they want, which may not be the case. Also, using move_and_slide() but discarding the return value is usually not a good idea.

Read the “Using KinematicBody2D” link I gave, and decide for yourself what type of movement you need.

kidscancode | 2020-11-20 19:21

sorry about that I forgot the var movement = move_and_slide(etc). I just assumed he would want move and slide, because his code looks like a top down game, but I could be wrong.

Millard | 2020-11-20 21:44

It was, yes. A verrrry simple little 2d game. It was primarily just to see if I could get the input controls to work. Thankfully, I finally did get it to work!

My initial code was kind of a slapdash of Input, and “Then do this.” so I don’t blame the computer for not knowing what I was trying to tell it.

At first the input scheme looked like:

if Input.is_action_pressed(“ui_forward”):
velocity.x += 1

Which, unfortunately, did not work. Instead I had to take the input in to two parts:

extends KinematicBody


export var speed = .10
var jump_speed = -.10
var gravity = -100
var velocity = Vector3()


func get_input():
var forward = Input.is_action_pressed(“ui_forward”)

var back = Input.is_action_pressed("ui_back")

var left = Input.is_action_pressed("ui_a")

var right = Input.is_action_pressed("ui_d")

var jump = Input.is_action_pressed("ui_jump")


if is_on_floor() and jump:
	velocity.y += jump_speed
if forward:
	velocity.x -= speed
if back:
	velocity.x += speed
if left:
	velocity.z += speed
if right:
	velocity.z -= speed


func _physics_process(delta):
velocity.y += gravity * delta
velocity = move_and_slide(velocity, Vector3(0, -.10, 0))

func _ready():

I used a pretty basic 2d guide from Godot’s docs and repurposed it to build a small 3d game now. Thus far, it’s carried over pretty well. Controls are a little jank, but that’s just more to learn.

Khan | 2020-11-21 01:28

just so you know, you didn’t have to assign the input to variables. you could have written:

if Input.is_action_pressed("forward")
    velocity.x -= speed

instead of

if forward:
   velocity.x -= speed

if you wanted to, and it would have worked. What was wrong with your original code, was that all it did was define a variable called velocity, and changed it’s value based on your input. The computer can’t know that you want the velocity variable to affect the characters movement. That’s why you needed to add the move_and_slide() function. That function is built by the devs to take a vector and transfer it to movement, and thus your computer knows what to do. Sorry if that was long, just wanted to clarify some stuff for you, hope it was clear. good luck with your future game development, keep at it! =)

Millard | 2020-11-21 04:00