this is the code im running:
bullet = bullet_scene.instantiate()
bullet.create(damage, knockback, projectile_speed, direction)
add_child(bullet)
create() only assigns those variables to the object and the bullet simply has position += self.direction * delta in its physics process. I would expect it to spawn and move in a straight line, at a fixed speed yeah? it moves a little (one chunk of projectile_speed) then stops for 30 loops of physicsprocess, then resumes moving normally. physicsprocess is still looping at this time, just not moving the object. genuinely what is going on here i cannot think of any reason for this?? any help appreciated, thanks <3
#Instantiated Packed Scene behaving weirdly
22 messages · Page 1 of 1 (latest)
If self.direction is a unit vector (vector with a length of 1), then it's going to be going extremely slowly (one pixel per second). Your projectile_speed isn't factoring into your position calculations. If projectile_speed is the number of pixels per second you want the bullet to travel, you'd want to multiply that to self.direction * delta to get the change in position over delta seconds.
self.direction is already multiplied by the speed!!
just a bad var name haha, its the direction to the cursor * speed
Okay, maybe show a larger part of your bullet code? Also, what kind of node is the root node of the bullet scene?
the bullet extends RigidBody2D
extends Projectile
class_name Bullet
func create(damage,knockback,speed,direction):
self.damage = damage
self.knockback = knockback
self.speed = speed
self.direction = Vector2(direction * speed)
func _physics_process(delta):
position += self.direction * delta
extends RigidBody2D
class_name Projectile
var damage : int = 0
var knockback : int = 0
var direction : Vector2
var speed : float = 10
thats all the code for the bullet
Issue with rigid bodies is you shouldn't directly control its position and such; that's not how it's intended to be used
They're intended to be used by apply physics to it
If your bullets are simple and travel in straight lines without being affected by gravity and stuff, you can consider just implementing it with Area2D
i want to have bullet as a pretty versatile class with some having gravity
but i can implement that without rigidbody i guess
shall i try just dropping it to a Node2D or something and see if it fixes?
You can still use RigidBody if that better suits your purposes. Just use the RigidBody builtin functions for applying forces/impulse and whatnot
Instead if directly setting position
RigidBody automatically sets position based on the physical forces so if you try to set position, RigidBody builtin behavior will fight you
ohh okay, good to know. i think ill drop the rigidbody and only use that when i need it
thanks so muchhh ill try that now <3
Gravity could also be implemented without RigidBody just by having an accelerating downward velocity similar to platformer characters
omgg it works ahaha THANK YOU