Hi I try to make a shotgun bullet and I want to decide to change the size of the dispersion and a damge reduce by distance the bullet do and fix it minimum and max damage to 1 for the reduce damage by distance.

script for bullet 1:

``````extends Area2D
``````

const speed = 900

export(int) var damage = 10
var velocity = Vector2(1,1)

func shootatmouse(startpos):
self.global
position = startpos
var direction = (get
globalmouseposition() - startpos).normalized()
self.linear
velocity = -direction * speed

func physicsprocess(delta):
velocity.x = speed * delta
velocity.y = speed * -delta
translate(velocity)
setposition(getposition()+velocity*-delta)

script bullet 2:

``````extends Area2D
``````

const speed = 900

export(int) var damage = 10
var velocity = Vector2()

func shootatmouse(startpos):
self.global
position = startpos
var direction = (get
globalmouseposition() - startpos).normalized()
self.linear
velocity = direction * speed

func physicsprocess(delta):
velocity.x = speed * delta

``````translate(velocity)
set_position(get_position()+velocity*delta)
``````

script bullet 3:

``````extends Area2D
``````

const speed = 900

export(int) var damage = 10
var velocity = Vector2(-1,1)

func shootatmouse(startpos):
self.global
position = startpos
var direction = (get
globalmouseposition() - startpos).normalized()
self.linear
velocity = -direction * speed

func physicsprocess(delta):
velocity.x = speed * delta
velocity.y = speed * delta
translate(velocity)
setposition(getposition()+velocity*-delta)
Thank for helping me :)

edited Jul 29

Hello! Why you have 3 different bullet nodes?

yeah a use three bullet to give the "effect" of dispersion of a shotgun bullet.It just a part of the design of the weapon.

Edit:actually I find the solution for the dispersion but anyway could you just explain me how to to the damage reduce by distance whit a max damage and a minimum damage.

You could make variable damage like this:

``````var damage = 10.0
const damage_minimum = 1.0
const damage_reduction = 5.0

func _process(delta):
damage -= damage_reduction * delta
if damage < damage_minimum
damage = damage_minimum
``````

Then play around with the values of the variables to make an effect you like.
The longer the bullet will fly, the less the damage will it make, down to a minimum of 1.

You could also adjust the function to be nonlinear by, for example, reducing damage by a percentage instead of fixed value.

answered Jul 31 by (62 points)
edited Jul 31