AI script problems, interaction with grenade not working, need help.

Ok so basicly these are two of many functions that make up the AI script. Essentially these two functions are designed to get the player to: A) Locate the grenade B) Turn towards it briefly C) Then turn 180 degrees and run off avoiding the grenades explosion.

The idea is to simulate a 'Holy shit, it's a bloody grenade" moment. Now the second function 'FindClosestGrenade' work fine, however what you see in AvoidGrenade is not even close to working. How in hell do i write this function ? Essentially i wanna how to do the turn towards the grenade that function FindClosestGrenade finds and then run away. The invoking of this behaviour and all that i can do fine. Please help :)

function AvoidGrenade(){
    print("Message Received");
    var TargetGrenade = FindClosestGrenade();
    var grenadedirection = TargetGrenade.transform.position - transform.position;
    var from = transform;
    var to = TargetGrenade;
    var TurnRate = 1;
    SendMessage("SetSpeed", 0.0);
    transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(grenadedirection), TurnRate * Time.deltaTime);
    //transform.rotation = Quaternion.Slerp(from.transform.rotation, to.transform.rotation, Time.time * TurnRate);
    transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);

    yield new WaitForSeconds (2.5);
    transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(grenadedirection), rotationSpeed * Time.deltaTime);
    transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0);
    var forward = transform.TransformDirection(Vector3.forward);
    var speedModifier = Vector3.Dot(forward, grenadedirection.normalized);
    speedModifier = Mathf.Clamp01(speedModifier);
    direction = forward * speed * speedModifier;
    GetComponent (CharacterController).SimpleMove(direction);
    SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver);
function FindClosestGrenade() : GameObject {
    var ExistingGrenadeArray : GameObject[];
    ExistingGrenadeArray = GameObject.FindGameObjectsWithTag("ActiveGrenade");
    var ClosestGrenade : GameObject;
    var Distance = Mathf.Infinity;
    var position = transform.position;
    for (var ExistingGrenade : GameObject in ExistingGrenadeArray) {
        var Difference = (ExistingGrenade.transform.position - position);
        var curDistance = Difference.sqrMagnitude;
        if (curDistance < Distance) {
            ClosestGrenade = ExistingGrenade;
            Distance = curDistance;
    return ClosestGrenade;

Thanks in Advance,

Cameron Leafe

You mention you want to:

A) Locate the grenade B) Turn towards it briefly C) Then turn 180 degrees and run off avoiding the grenades explosion.

And state:

what you see in AvoidGrenade is not even close to working

However, you do not describe by what's happening that makes it "not even close to working". Is it that the effect is unrealistic? Is it crashing? Is it going bonkers?

As to how to write it, there are many possible approaches. One would be:

  1. Get a vector from the grenade towards your character
  2. Extend a vector from your character's position for whatever the character feels is a safe distance
  3. Flee towards that vector at an increasing speed (as he will need to accelerate)

I'm not sure why you'd turn 180 degrees, unless your character's whole body is turning towards the grenade - bear in mind that a human being would likely just turn their head, unless the grenade they perceived fell more than 90 degrees from their current heading. This means that in order to flee it, it would be unnecessary and inefficient to turn 180 degrees from their current orientation.

Try using transform.LookAt(Target)!