projectile behavior

devastatingT1999devastatingT1999 Posts: 7Member
edited June 18 in Programming

how to make projectile stop when it detect collision with anything it collide with,this is my code for now:

extends KinematicBody2D

var velocity = Vector2.ZERO
const THROW_VELOCITY = Vector2(7,0)
func _physics_process(delta):
    if Input.is_action_pressed("shoot"):
        velocity = THROW_VELOCITY

    var collision = move_and_collide(velocity * delta)
    if collision != null:
        velocity = 0

Comments

  • TwistedTwiglegTwistedTwigleg Posts: 4,589Admin

    I would suggest using a boolean that tells the projectile whether it's collided or not:

    extends KinematicBody2D
    
    var velocity = Vector2.ZERO
    const THROW_VELOCITY = Vector2(7,0)
    var has_collided = false
    
    func _physics_process(delta):
        # Do nothing if the bullet has collided already
        if (has_collided == true):
            return
    
        if Input.is_action_pressed("shoot"):
            velocity = THROW_VELOCITY
    
        var collision = move_and_collide(velocity * delta)
        if collision != null:
            has_collided = true
    
  • newmodelsnewmodels Posts: 479Member

    Also, using the collision bool flag, I would also call .free() on itself. So the bullet would free itself, delete. All code after the free function is called must end. So it has to return from there.

  • devastatingT1999devastatingT1999 Posts: 7Member

    @TwistedTwigleg said:
    I would suggest using a boolean that tells the projectile whether it's collided or not:

    extends KinematicBody2D
    
    var velocity = Vector2.ZERO
    const THROW_VELOCITY = Vector2(7,0)
    var has_collided = false
    
    func _physics_process(delta):
      # Do nothing if the bullet has collided already
      if (has_collided == true):
          return
      
      if Input.is_action_pressed("shoot"):
          velocity = THROW_VELOCITY
          
      var collision = move_and_collide(velocity * delta)
      if collision != null:
          has_collided = true
    

    thank you very much

  • devastatingT1999devastatingT1999 Posts: 7Member

    @newmodels said:
    Also, using the collision bool flag, I would also call .free() on itself. So the bullet would free itself, delete. All code after the free function is called must end. So it has to return from there.

    the game i aim for is where the weapon teleport the player to it position so i don't think i need this function + also thanks the code was really helpfull

  • TheDiavoloTheDiavolo Posts: 66Member

    @devastatingT1999 said:

    @TwistedTwigleg said:
    I would suggest using a boolean that tells the projectile whether it's collided or not:

    extends KinematicBody2D
    
    var velocity = Vector2.ZERO
    const THROW_VELOCITY = Vector2(7,0)
    var has_collided = false
    
    func _physics_process(delta):
        # Do nothing if the bullet has collided already
        if (has_collided == true):
            return
        
        if Input.is_action_pressed("shoot"):
            velocity = THROW_VELOCITY
            
        var collision = move_and_collide(velocity * delta)
        if collision != null:
            has_collided = true
    

    thank you very much

    in this case in specific what I would do is disable the physics_process with set_physics_process (false)

    if i had more code that needs to be executed yes or if i would not, or i would change it to _process

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file