#im having a bit of trouble with projectile shooting

1 messages · Page 1 of 1 (latest)

white quiver
#

the projectile script:


@export var speed: float = 0
@export var damage: int = 10

func _ready():
    linear_velocity = -transform.basis.z * speed```
the part of the player script that instantiate the projectile.
```var InstantiateDagger = DaggerProjectile.instantiate()
InstantiateDagger.position = Shootpoint.position
InstantiateDagger.rotation = Shootpoint.rotation
get_parent().add_child(InstantiateDagger)```
when i trigger the shoot function absulutly nothing happens.
#

also here is the player tree and the shootpoint position

dense palm
#

Did you try placing a breakpoint to confirm the function is being called?
Also, could you be confusing global with local positions?

white quiver
#

i print debugged it and the function is being called. should i set the projectiles global position and rotation to the shootpoints global position and rotation?

dense palm
#

Probably, just to eliminate the possibility of relative positions messing with stuff

white quiver
#

i just tried it and the same result. nothing visible happens

#

i am also getting these errors now

dense palm
#

What is in line 35 and 36?

white quiver
#
InstantiateDagger.global_rotation = Shootpoint.global_rotation```
dense palm
#

Ah, try doing add_child() before setting that.

#

This all happens mid frame, so it won't be visible for the player.

white quiver
#

ok so now the projectile does spawn, but it just falles through the ground. it doesnt have velocity

#

oh yeah thats becuz i set the speed to 0 to see if the problom was that it was moving too fast i couldnt see it

#

ok so if i set the speed var in the projectile script it has velocity twards the Z axis. how do i make it fly in the direction its facing

dense palm
#

You can get the facing direction with global_transform.basis.z iirc

#

So velocity = global_transform.basis.z * speed

white quiver
#

oh yeah that makes sense

#

the projectile is a rigid body

#

so it has an error "velocity is not declared in the current scope"

#

should i make the projectiles into a characterbody?

white quiver
#

nvm i found the solution.

in proccess function

dense palm
#

I recommend running it in _physics_process()
Specially for a RigidBody

white quiver
#

whats the difference?

dense palm
#

As it is, i am not sure if it will work due to RigidBody relying on applying forces.
If it gives you trouble, turn it into an AnimatableBody or a CharacterBody

#

_process() runs once per frame. Which can vary and it is not synched to physics.
_physics_process() runs at a set pace and is synched to physics.

#

It can cause jittery movement, it can break on lower or higher FPS than intended, etc.

white quiver
#

ill give it a try, thanks

#

it doesnt break or anything, but no the projectiles have gravity, which i do not want

dense palm
#

Then you are def. better off with either of the other 2

white quiver
#

so change it to characterbody?

#

i just changed the gravity scale on the rigidbody to 0 and it all works

white quiver
#

ok so im trying to make the projectile slightly homing. ``` var target_rotation = global_position.direction_to(nearest_enemy.global_position)
rotation.x += clamp(target_rotation.x, -0.1, 0.1)
rotation.y += clamp(target_rotation.y, -0.1, 0.1)
rotation.z += clamp(target_rotation.z, -0.1, 0.1)

dense palm
#

clamp() restricts the first parameter between the second and third.
This would make your input, regardless of what it is, unable to go past -0.1 or 0.1

#

If you want to rotate slowly. Get the amount that you want to rotate by and use:

rotation = rotation.move_toward(target_rotation, 0.1)

With 0.1 being the amount it should rotate per call.

white quiver
#

its still funky. it moves in a spring shape forward. so it does circles but also moves forward

#

btw this is the find nearest enemy thing for enemy in get_tree().get_nodes_in_group("Enemy"): var distance = global_transform.origin.distance_to(enemy.global_transform.origin) if distance < nearest_distance: nearest_enemy = enemy nearest_distance = distance

dense palm
#

I have to go rn, but 3D rotations are actually a Vector4, so it can behave funky when you use the simplified rotation properties.
Only help i can provide rn is to try calling orthonormalize() on the node whenever you rotate it, that has fixed stuff for me in the past.

white quiver
#

it didnt work, but i think ill be fine from here, ill lookup a bunch more tutorials and basically just try stuff out until something works. thanks for the help so far ❤️

white quiver
#

ok big discovery. i can use look_at() to set the desired location

#

also the problom was that i didnt find the correct rotation to begin with

white quiver
#

ok so ive made a bunch of discoverys. direction and rotation are different things, therefore setting my rotation to the results of direction_to results in some funky stuff

#

now i just need to find out how to make an object slowly look in a cetein DIRECTION

#

ok i just figured it out yayyyyyyyyyy

            var current_direction = -transform.basis.z.normalized()
            var new_direction = current_direction.slerp(target_direction, 7 * delta)
            look_at(global_transform.origin + new_direction, Vector3.UP)```
now another problom. if i shoot a projectile at the right angle at the right position, it will forever circle the target
dense palm
#

Sounds like the turn rate is too small for the speed it is travelling at.
Many games get that issue, most either set a timer so it despawns in those edge cases OR increase the turn rate the faster they move or the longer they exist.

#

Or you can do it like WC3 and just make it teleport to the target if it cannot reach it for long enough.

white quiver
#

I think I'll go with the send option

#

Second

#

Where the longer it's alive the bigger the turn rate