Knocked back by enemies

Hi guys,

trying to help a student figure this out - we have a spiked log in a medieval level that hurts you when you hit by it - it kinda patrols back and forth. So because it hurts you and takes away health points, we need to have the player pushed away from the enemy log else it loses way too many points - plus it would be a nice effect - currently have this -

var startPos : GameObject;
var key: GameObject;

function Update () {

        if(transform.position.y < -10.0){

        transform.position = startPos.transform.position;

        }
}

Which handles our player falling off the map (its an fps controller by the way). And so I tried to take the same principle into this -

var ouchAmount = 1.0;
var ouch : AudioClip;

var startPos : GameObject;

function OnControllerColliderHit (hit : ControllerColliderHit) {
       
           if (hit.collider.gameObject.tag == "log") {     

                var thePlayer = GameObject.FindWithTag("Player");
                thePlayer.transform.position = startPos.transform.position;

                Health.health -= ouchAmount;
           
            var owpoint = hit.collider.transform.position;
        AudioSource.PlayClipAtPoint(ouch, owpoint); 
               
                       
           }
}

which does not work, presumably because unlike the first script, im not within an update function. Can anyone advise??

Many thanks,

Will

You appear to be using the right approach. Does the script do anything at all right now? Try putting print() calls in to see whether the collisions are actually being detected.

Can you make the spiked log bouncy?
AC

Guys,

thanks to you both for your replies, I will get back on this on monday and try making the log bouncy, is that with a Physic material? I assume its like using “reticulation” in Shockwave 3D?.

Muriac, no the script does nothing at the moment, but the health is being taken off so its definitely detecting the collision… odd…

Cheers,

Will

Calculate the vector of the direction the player should move being pushed by the log, and then add it to the player’s position multiplied by a constant that will be the distance.

var away = (player.transform.position - log.transform.position).normalized;

player.transform.position += away * 3.0;

That should make the player move away from the damaging object, if the player use physics, then change the assignment for a force function.

Omar Rojo