x


AI controller script, Enemy movement issues

I'm new here, so bear with me.

Essentially the issue is that the "enemy" right now, just a cube, is moving towards the character as it should, however, it doesn't recognize when to stop moving towards the character. When I used the print function to debug, the only messages that showed up are "too close", and "in range". These messages were constantly active. No matter what I tried, the cube just moves towards the character, and when it hits the character, it initially will nudge at the character, but when the character moves, it proceeds to go inside the character, constantly nudging, and shaking, sometimes throwing it across the map.

I have set up a boolean setup, as shown, to make sure it is formatted cleanly and debugging is made easier. Here is the script:

var sightRange = 75.0;
var idealRange = 5;
var speed = 15;
var startAttackrange = 15;
var spacebtwn = Vector3.Distance(transform.position, player.transform.position);
var player = GameObject.FindWithTag("Player");
var delayTime = 0.0;
var backspeed = 10;
var rateofFire = .75;
var initialSpeed = 30;
var projectile : Rigidbody;

function TooClose()
{
    if(spacebtwn < idealRange)
    {
        print("tooclose");
        return true;
    }
    else return false;
}

function InRange()
{
    if(spacebtwn <= sightRange)
    {
        print("In range");
        return true;
    }

    if(TooClose == true)
    {
        print("inrange is false");
        return false;
    }

    if(spacebtwn > sightRange)
    {
        print("out of range");
        return false;
    }
}

function AttackRange()
{
    if(spacebtwn >= idealRange && spacebtwn < sightRange)
    {
        return true;
    }
    else return false;
}

function Update()
{
    if(InRange() == true)
    {
        var direction = player.transform.position - transform.position;
        transform.Translate(Vector3(0, 0, 1) * speed * Time.deltaTime);
        transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction),  Time.time);
    }

    if(TooClose() == true)
    {
        transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction),  Time.time);
    }

    if(AttackRange() == true)
    {
        print("attacking");
        Attk();
    }
}

function Attk ()
{
    if (Time.time > rateofFire)
        {
            // create a new projectile, use the same position and rotation as the Launcher.
            var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

            // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
            instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

            // Ignore collisions between the missile and the character controller
            Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

            lastShot = Time.time;
        }
    }
more ▼

asked Dec 04 '10 at 11:48 PM

Jordan 7 gravatar image

Jordan 7
12 2 2 4

Just some scripting tips... Use whole words for your scripts... And don't put cuss words in... Maybe add some comments too...

Dec 05 '10 at 12:01 AM Justin Warner

I am so sorry that was left in there it was a joke between me and my friend as we got frustrated trying to fix this. I just edited it out. To anyone who read this script before I did edit it out I am so sorry and thank you for viewing and trying to help.

Dec 06 '10 at 12:40 AM Jordan 7
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3465
x3340
x1373
x960
x655

asked: Dec 04 '10 at 11:48 PM

Seen: 2099 times

Last Updated: Dec 06 '10 at 12:38 AM