Sun : Damage if too close, damage if too far.

Hi everyone !

I am pretty new to javascript and unity but slowly learning :slight_smile: !

I have made a plant, able to grow, change model thru it’s life, reproduce itself, and die. It took me 2 days but now it finally works :smiley: !

The next step for me is to make the plant have a certain amount of life that would go down when too close or too far from a “sun” (just a light). I want the damages to get more and more important according to distance.

I have absolutely no idea how to do this.

Any help is welcome :slight_smile: ! Thanks in advance.

you can use Vector3.Distance

maybe something like this (i use javascript for this script)

var Plant :Transform;
var maxDamage :float;
var health :float = 100;
private var realDamage :float;
function Update()
   health -= realDamage*Time.deltaTime;
   realDamage = maxDamage - (Vector3.Distance(transform.position, Plant.position));

if(health <= 0)
  //do something in here

maybe its can help you …