- Home /
How can I ground enemies and use target transform?
Hello game developers, I have a question. I have an enemy character that is targeting my player character and when it follows me the enemy is floating like a ghost. my question is can somebody explain to me how to ground the enemy and also help me out with my script please?
the following is my monsterAI script that is attached to the enemy.
var distance; var target : Transform;
var lookAtDistance = 15.0; var attackRange = 10.0; var moveSpeed = 5.0; var damping = 1; private var isItAttacking = false;
var enemySound : AudioClip;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
renderer.material.color = Color.yellow;
lookAt ();
}
if(distance > lookAtDistance)
{
renderer.material.color = Color.green;
}
if(distance < attackRange)
{
attack ();
}
if(isItAttacking)
{
renderer.material.color = Color.red;
}
}
function lookAt () { var rotation = Quaternion.LookRotation(target.position - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); }
function attack () { isItAttacking = true; renderer.material.color = Color.red;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
audio.PlayOneShot (enemySound);
}
@script RequireComponent (AudioSource)
Answer by ICHeeryI · Sep 03, 2013 at 03:40 PM
,,How can I ground enemies?,, You can use:
http://docs.unity3d.com/Documentation/Components/class-CharacterController.html
Thank you very much! I just learned something new and very useful.
No problem! If this is correct answer, please accept it.