Enemy AI problem

Hey people,

Currently i,m working on a fast paced action RPG and am stuck on the AI of the enemy.
Whenever i enter it’s "aggro"range it automatically comes at me ( as it should) but then
then the following happens depending on which imported mesh i have active:

  • Old enemy model with attached script: Wont find / play walking animation as soon as it starts moving( hasnt been propperly imported due to it’s pivotpoint being backwards so i placed it in a gameobject to fix that)

  • new enemy model with attached script: Does play animation but first jumps away ( half through the floor) and then comes closer. when it’s at a closer range ( yet further than the other model) it starts spinning around the character in all 3 dimensions(old model only spinned in 2 dimensions and litterally around the character). I’ve tried adding a rigid body to it but it instantly
    Decided to fall through the floor on me. Ofc the ground has a mesh collider on it so it’s rather strange the enemy mesh doesnt recognize it.

Could anyone have a look at the code and see what i’ve done wrong as i preffer to use the newer model since i,m going to have to clean up/edit and add some more animations at a later stage on the enemy.

P.s the only small edit i’ve made when putting the code on the newer model is that i could get rid of the var on line 38 and could put an animation.play(“Running”)

var target : Transform; //the enemy’s target

var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning

//var myTransform:Transform; //current transform data of this enemy

static var isTriggered = false;
static var Playerhitbox = false;

function Awake()
{
//myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
//if isTriggered = true;
target = GameObject.FindWithTag(“Player”).transform; //target the player

}

function Update ()
{
if(isTriggered)
{

//rotate to look at the player
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position-transform.position), rotationSpeed*Time.deltaTime);

//move towards the player
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

var Enemy_animation = GameObject.Find(“Enemy_bugged”);;
//var Enemy_animation = getComponent(“Enemy_bugged”);; tried wouldnt work
// Enemy_animation = GameObject.FindWithTag(“Enemy”) tried wouldnt work
// Re-imported Object flipped in max and got even stranger bugs…

// next line continueation of what should be working above this.
//Enemy_animation.animation.Play(“Running”, PlayMode.StopAll);
}
//if(Playerhitbox) enemy should stop and attack
}

Thanks in advanced

Digi

Mesh colliders don’t collide with other concave mesh colliders (something like that.)

GameObject.Find( “X” ) finds only the first object named exactly X

object.GetComponentInChildren( Animation ) finds the first Animation component in the current object’s hierarchy - I’m guessing you’ll want to switch to this instead of .Find.

The odd behaviour looks to be a combination of incorrect pivot point on the controlling transform and/or target object, and/or bones not having keys set, or having keys set when they shouldn’t, in your animation program.

I have tried the GameObject.find but that wouldnt help, I,ll try GetcomponentInChildren a bit later and hope it’s the solution.

as for the odd behaviour that was with the enemy “mesh” with a correct pivot point though it might have added
some keys where there shouldnt be keys ( made it with 3ds max > cat objects) though it seems a bit strange to affect the rotation of the whole object around the character instead of just that part ( animation in this case).
while with the old mesh which is made in the same way it doesnt ( so the old mesh only spins around the mesh in 2 angles instead of in 3 angles with the good mesh ( pivot point was set correct here) .

transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);

you want transform.forward, not Vector3.forward.

transform.forward is local forward, Vector3.forward is world +Z.