|
when the enemy or multiple enemys gets in range of the melee swing then send them a adjust health command to remove some hitpoints.. i really cant get the hang of unitys way of doing things / syntax . can someone help me out thanks
(comments are locked)
|
|
The easiest way is like you said: remove health from the enemy when the player attacks AND the enemy is in the attack range. You can find all enemies in the range using Physics.OverlapSphere: it returns an array with all colliders whose bounds touch an imaginary sphere of given radius. This must be used just as an approximation: bounds are boxes aligned to the world axes, thus the attack range may be up to 70% larger depending on the enemy position. To avoid this, you must verify if the objects hit by the sphere are inside the attack range; if yes, use SendMessage (see below) to apply the damage.
var range: float = 1.8;
var attackInterval: float = 0.7;
var meleeDamage: float = 30;
private var nextAttack: float = 0;
function MeleeAttack(){
if (Time.time > nextAttack){ // only repeat attack after attackInterval
nextAttack = Time.time + attackInterval;
// get all colliders whose bounds touch the sphere
var colls: Collider[] = Physics.OverlapSphere(transform.position, range);
for (var hit : Collider in colls) {
if (hit && hit.tag == "Enemy"){ // if the object is an enemy...
// check the actual distance to the melee center
var dist = Vector3.Distance(hit.transform.position - transform.position);
if (dist thanks alot this will really help me !
Dec 11 '11 at 09:40 PM
slooie2
(comments are locked)
|

i just want to detect if the enemy collided with the player. and then id use a get component to see if attacking was true then adjust the health but i cant figure out the collider commands
It's explained very clearly HERE: http://unity3d.com/support/documentation/ScriptReference/Collision.html and HERE http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html?from=Collision and on many other pages.