2
\$\begingroup\$

In Unity, I have created an enemy AI and applied a script for it to move and chase the player. But when moving, the enemy hovers in the air. My character also does not have a rigid body, he just has character controller and my enemy AI is child object and an empty object as parent object. I added this to do animation. This is my enemy AI script created in C#:

using UnityEngine;
using System.Collections;

public class AdvancedAi : MonoBehaviour

{
    float distance;

    public float lookAtDistance, chaseRange, attackRange, moveSpeed, damping, gravity, attackRepeatTime;
    public Transform target;
    public CharacterController controller;

    private Vector3 moveDirection = Vector3.zero;
    private float attackTime;

    //TODO: add in a function to find controller and to locate and assign the player as the target

    void Start()
    {
        attackTime = Time.time;
    }

    void Update()
    {
        distance = Vector3.Distance(target.position, transform.position);

        if(distance < lookAtDistance)
        {
            LookAt();
        }

        if(distance > lookAtDistance)
        {
            GetComponent<Renderer>().material.color = Color.green;
        }

        if (distance < attackRange)
        {
            AttackPlayer();
        }

        else if (distance < chaseRange)
        {
            ChasePlayer();
        }
    }

    void LookAt()
    {
        GetComponent<Renderer>().material.color = Color.yellow;
        Quaternion rotation = Quaternion.LookRotation(target.position - transform.position);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
    }

    void ChasePlayer()
    {
        GetComponent<Renderer>().material.color = Color.red;

        moveDirection = transform.forward;
        moveDirection *= moveSpeed;

        moveDirection.y -= gravity * Time.deltaTime; // doesn't seem to work right :(
        controller.Move(moveDirection * Time.deltaTime);
    }

    void AttackPlayer()
    {
        //TODO: Need Attack Animations
        if (Time.time > attackTime)
        {
            Debug.Log("Attack");
            attackTime = Time.time + attackRepeatTime;
        }
    }

    void ApplyDamage()
    {
        chaseRange += 30;
        moveSpeed += 2;
        lookAtDistance += 40;
    }
}
\$\endgroup\$
2
\$\begingroup\$
CharacterController controller

Has a radius and height that define its space occupation. (the green cage in the image). That cage (capsule) interact with othe colliders (i.e : the terrain). If the cage its too big respect the enemy shape, you see it fluctuate, because the cage bottom touches the ground.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ thank you for the help. its works now and liked your answer \$\endgroup\$ – nirmal Jul 30 '15 at 10:42
0
\$\begingroup\$

Your todo can be done with, FindGameObjectWihTag ("Player") as there will only ever be one player per instance unless you're doing multi-player.

Damping has no value.

You need a value for gravity on the empty otherwise you have 0 gravity and you're reducing to <0 which would cause it to float.

\$\endgroup\$
  • \$\begingroup\$ Well its singleplayer and I changed my gravity to 10,20,30,40,100,1000 even 100000 but still it does not touch the ground when moving and when changed gravity to -3 it flys higher so no use and also changed damping to 6 but still does not work \$\endgroup\$ – nirmal Jul 30 '15 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.