Could someone explain to me the code in the last two line? I’m a bit confused by it. Also is hit a gameobject because it was referenced as a variable. That is why its able to access transform?
var TheDamage : int = 50;
var Distance : float;
function Update ()
var hit : RaycastHit;
if(Physics.Raycast (Transform.position, transform.TransformDirection(Vector3.foward),hit))
Distance = hit.distance;
hit.transform.SendMessage("ApplyDamage"), The Damage, SendMessageOptions.DontRequireReceiver;
In you case
- when you click, a ray is send from position “Transform.position” in forward direction.
- RaycastHit “hit” returns you all the details about the object your ray hit.
- " hit.distance " returns distance from the ray’s origin to the impact point i.e enemy in ur case or in other words “how far the gameobject/enemy is far from ray’s origin( your gun aim)”
hit.transform.SendMessage("ApplyDamage", The Damage, SendMessageOptions.DontRequireReceiver) this will call the method
ApplyDamage(float damage_amount) which in some other script
I have assumed that ApplyDamage() has a float type argument