- Home /
AI enemy bouncing with no collision
Hello, I have been hunting and have found a script that works in short, I would prefer if my enemy would avoid walls but I could easily work around this. My problem is that the enemy will not come close enough to my player to look like it has actually attacked me. It is almost like there is a cushion around my player but I have checked all colliders and they are no way near each other when the enemy pushed back:
var enemy : Transform;
var player: GameObject;
var dir: Vector3;
var speed: float;
var observed: boolean;
var sightDistance: int;
function Update () {
if(Vector3.Distance(enemy.position, transform.position)< sightDistance){
transform.LookAt(enemy);
if (observed){
dir = player.transform.position - transform.position;
dir = dir.normalized;
transform.Translate(dir * speed, Space.World);
}
else {
transform.eulerAngles.y = Mathf.PingPong(Time.time*20,90) -45;
}
}
}
function OnTriggerEnter(other:Collider) {
if(player) observed =true;
}
function OnCollisionEnter(collision : Collision)
{
if(collision.collider.gameObject.name == "arrow(Clone)")
{
Destroy(this);
}
}
Any help on the matter would be great!
Cheers, Matt
Answer by Griffo · Aug 27, 2012 at 01:22 PM
Here is a script to watch, follow and attack player, it might help you out.
#pragma strict
var distance : float = 20;
var target : Transform;
var lookAtDistance : int = 30.0;
var attackRange : int = 10.0;
var moveSpeed : int = 3.0;
var damping : int = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position, transform.position);
if(distance < lookAtDistance)
{
isItAttacking = false;
lookAt ();
}
if(distance < attackRange)
{
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
function attack ()
{
isItAttacking = true;
transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}
I tried this but it had a material error. Thankyou for the response though! What I think I need is some sort of way to allow the enemy to come to collision with the player, if that is impossible with my script I will need to revamp the script completely, so if you can tell what I am doing wrong with your script I would appreciate it!
I tried this script but it didn't seem to do anything. I assume its because I have no renderer on my enemy. All I want is for the enemy to come to contact with the player, it already follows when in range.
Thanks for the quick reply though and if it is me that is making an error with your code, please explain as I am open to new ways of following if that will fix the problem!
I've edited my answer above, but a cube in the scene, put the script onto it and if you move towards it, it will chase you, you then can add a trigger to do stuff when it either collides with you or get within a distance you set ..
Thankyou so much, its a much tidier script than my previous one too! you have been a great help!
Glad I could help .. Please mark it as answered .. Thanks.
Your answer
Follow this Question
Related Questions
Enemy Ai Problem with the collider and gravity 1 Answer
enemy AI avoiding collision 1 Answer
Precise hitboxes for characters 1 Answer
Enemy AI don't collide with objects or rotate. 0 Answers
Player pushback when collide with enemy 2 Answers