- Home /
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;
}
}
Just some scripting tips... Use whole words for your scripts... And don't put cuss words in... $$anonymous$$aybe add some comments too...
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.