Okay, so I looked at the code, and I didn't see anything that seems to be the problem. The code you posted is for the player, correct? I think the problem is in the gun/rifle, and after taking a look at the code I provided, I think I know what is causing the problem and how to fix it.
The problem you are having is when you fire a bullet and move the player the bullet moves too, right? So if you fire straight, and them move left, the bullet moves left as well?
If this is what is happening, and you are using the code I posted earlier, then I know what's going on. In the gun script we are adding the bullet as a child of the gun. This means any changes to the gun (position, rotate, and/or scale) will effect the bullets as well because they are children nodes of the gun.
So, there are a couple ways to fix this. The easiest way is to replace add_child(clone) in the gun script to get_tree().root.add_child(clone). This works pretty well and (I think) it is what I did in the FPS tutorial. There are some downsides though. One downside is it's hard to get the bullets again.
For example, if you need to clear all of the bullets in the scene, you will have to go through the entire scene tree, find the bullets, and them remove them. Not a major deal, but it's something to keep in mind.
Another thing you will want to add is some sort of destruction timer to your bullets, so if they do not hit anything after a certain amount of time, they will destroy themselves to save process speed and memory. You can do this by making a simple timer variable and adding delta to it every _process or _physics_process call.
Here are the revised versions of the example scripts I wrote above. I changed how the bullets are added to the scene (they are now added to the root instead of the gun) and I added a death timer to the bullets so they destroy themselves after 120 seconds.
shooting-thing/gun script:
`
extends Spatial
const BULLET_SCENE = preload("res://Bullet_Scene.tscn");
func ready():
pass
func physics_process(delta):
"Assuming you have a action for firing already setup"
if Input.is_action_pressed("fire"):
"Instance/create the bullet scene"
var clone = BULLET_SCENE.instance()
Add the clone as a child of root, since the root will not be moving.
get_tree().root.add_child(clone)
"Copy the rotation so the bullet faces the same direction as this node"
clone.global_tranform.basis = global_transform.basis
`
bullet script:
extends Spatial
const MOVE_SPEED = 10
" The length of time (in seconds) the bullet has been alive"
var kill_timer = 0
"The amount of time it takes (in seconds) before the bullet destroys itself"
const KILL_TIME = 120
func _physics_process(delta):
kill_timer += delta
if (kill_timer >= KILL_TIME):
queue_free()
"Get the local forward direction (the direction the blue arrow points to when in local axis mode)"
var forward_direction = global_transform.basis.z.normalized()
"Move the bullet forward according to the forward direction"
global_translate(forward_direction * MOVE_SPEED * delta)
@gelgavish said:
Thanks for supporting me so much!
It's my pleasure! I hope this helps!