#Instantiated Packed Scene behaving weirdly

22 messages · Page 1 of 1 (latest)

daring eagle
#

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

normal sand
#

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.

daring eagle
#

self.direction is already multiplied by the speed!!

#

just a bad var name haha, its the direction to the cursor * speed

normal sand
#

Okay, maybe show a larger part of your bullet code? Also, what kind of node is the root node of the bullet scene?

daring eagle
#

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

normal sand
#

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

daring eagle
#

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?

normal sand
#

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

daring eagle
#

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

normal sand
#

Gravity could also be implemented without RigidBody just by having an accelerating downward velocity similar to platformer characters

daring eagle
#

omgg it works ahaha THANK YOU