Player movement script not working

Hello, I have been following the course with everything working correctly up until we implemented apply_force(). For some reason I am not able to get the spaceship to move. I thought maybe there was just something off with that particular code so I kept moving forward and after setting up my input mapping for both the right arrow key and the D key I am still not able to make the spaceship fly to the right. Below are both of my scripts.

level.gd:

extends Node2D

# Called when the node enters the scene tree for the first time.
func _ready():
	print("Hello, world!")

player.gd:

extends RigidBody2D
	
func _physics_process(delta):
	if Input.is_action_pressed("move_right"):
		apply_force(Vector2(25, 0))

I am not sure if the issue is with my scripts or if it might have been with how I created the scenes and tied them together. I am going to be trying to duplicate the issue on a different computer as I am traveling so I will update this post if I resolve the issue before an answer is found.

I’ve compared your code to mine and it looks ok from here. I’ve got 300 as my magnitude, but I tested with 25 and there was still movement, so that’s not the issue.

In general, checking your control flow from beginning to end with print() statements is a great way to isolate issues when debugging; in this case, I’d put one inside the if-statement, and one directly inside physics_process() (there was a similar issue recently caused by the script not actually being attached to the node).

Have you implemented the other 3 directions yet?

Remember to include move_and_slide() at the end of your movement function in order to make it work as spected

I haven’t implemented the other move directions as I am not able to get the move_right to work properly. I didn’t want to continue until I was able to actually get the spaceship to move.

I was able to get things to print from the _physics_process and _process functions but when I tried to use the apply_impulse and apply_force methods they weren’t working.

Actually, move_and_slide() doesn’t exist on RigidBodies - that’s how CharacterBodies are moved. These forces work in a different way =)

I haven’t quite made it to that part. I stopped progressing through the lecture when I realized that something was really wrong. When I was trying to get the apply_impulse function to work and it wasn’t I continued to go through the lecture but when the input commands weren’t working when I had built another game where they were working just fine I knew something was wrong. I am just not sure where I went wrong.

ohh my bad. I implemented a different approach for the project

1 Like

So if I’m understanding correctly, this didn’t return a print statement:

func _physics_process(delta):
	if Input.is_action_pressed("move_right"):
        print("move_right pressed")
		apply_force(Vector2(25, 0))

If that’s the case, you might want to double-check your input mapping in the project settings. I made a few mistakes there myself at this stage ^v^

It’s also worth doing this for the moment to see if your force code is correct (remove the conditional):

func _physics_process(delta):
	apply_force(Vector2(25, 0))

No worries. Haha. Thank you for trying to assist anyways. :slight_smile:

So I just did the print statement again just to make sure that everything is working and when I press either the D key or the right arrow key “move_right pressed” is printed to the console. I think there might be something wrong with my scene and not my code.

Yes, given that we now know your code’s control flow works and your input mapping is done correctly, that seems pretty likely. Now we know more!

Can you post a screenshot of your scene tree, and maybe the Player’s Inspector as well for good measure?

Oh I just decided to start over. Test myself on what I have learned so far to see if I remember how to do the steps on my own. But if I run back into the issues I will post another question. Thank you for helping me troubleshoot.

2 Likes

Sure!! We all learn from each other!!

2 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms