- Home /
enemy falls through terrain - code attached
here is the enemy chase that im using in a game. for some reason when the enemy player moves towards my hero he sinks into the ground i think its because he "gravitates" towards the players feet (the xyz position of said feet) and it causes him to sink.... any suggestions ?
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target; // transform = the x,y,z, coordinates public int movespeed =1; public int rotationspeed =1 ; private Transform mytransform; public int attackdistance = 15; public Vector3 aggroradius;
void Awake ()
{
mytransform = transform; // caches the value of transform to optimize runtime (sorta)
}
// Use this for initialization
void Start () {
//finds the player
GameObject go = GameObject.FindGameObjectWithTag("Player");
// enemy target is the players x,y,z position...
target = go.transform;
}
// Update is called once per frame
void Update () {
//look at player
mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(target.position- mytransform.position), rotationspeed *Time.deltaTime);
//finds the distance between player and enemy
aggroradius = target.position - mytransform.position;
// if the positive value of the distance between the player and the enemy is less than the attack distance - hulk smash //jk
if (aggroradius.magnitude < attackdistance)
{
// move towards player
mytransform.position += mytransform.forward * movespeed * Time.deltaTime;
}
// do the funky chicken
animation.Play("walk");
// if (distance = good)
// attack ?
//animation.Play ("attack");
}
}
thanks !
Answer by Peter G · Aug 26, 2010 at 11:27 PM
I would not recommend creating a continuous movement by modifying the transform.position because it will clip through objects like the terrain unlike other methods such as CharacterController.Move(). But, to answer your question your character is not directly gravitating to his feet as in being pulled to the position, but is aligning himself to go into them, due to the way you have the character set up. Since the character aims "forward" at the target's feet and only moves forward, he will "gravitate" to the target's feet. The only reason I make that difference is because when I think gravitate I think being pulled in a direction arbitrary to your forward direction. Now, that might not be a good definition, but it was supposed to help clarify what part was causing the problem (the rotation).
The easiest, way to fix this would be to store the target's position in a Vector3 (note: Vector3 is valueType, Thats important otherwise we would move the target also.) Then make the y the same as the player.
i.e.
var aimPoint = target.position; aimPoint.y = transform.position.y;
mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(aimPoint), rotationspeed *Time.deltaTime);
Another way that would involve more work would be to set up your character to move using a CharacterController. This would take longer in the short time, but will make movement easier in the long run because you will not have to worry about missed-collisions. You can still do the rotation the same as in your old script the only major difference is moving the player.
var targetPos = target.position - transform.position;
targetPos = targetPos.normalized;
GetComponent(CharacterController).Move(targetPos * Time.deltaTime * moveSpeed);
You can cache a link to the character controller see the performance optimization page.
target = go.transform; aimpoint = target.position; aimpoint.y = transform.position.y;
i wanted to go with the top route you provided , and this is where i put the variables, (as comparing to my code above) the monster seems to stay on land without sinking but now seems to walk away from my "hero" ins$$anonymous$$d of walking towards him.. any ideas ? thanks :)
it works but the monster walks away from the hero ins$$anonymous$$d of towards him (trying to be more clear)
update
swapped mytransform.rotation = Quaternion.Slerp(mytransform.rotation, Quaternion.LookRotation(target.position- mytransform.position), rotationspeed *Time.deltaTime);
for
mytransform.LookAt(aimpoint); while adding
aimpoint= target.position; aimpoint.y = transform.position.y;
to the update function
:) thanks !
Edit: Glad to here you fixed your problem :) The mistake I made was this: var var aimPoint = target.position - transform.position; aimPoint.y = 0; So glad to here you fixed it, but that's the correction if you want it.