I created an enemy script to move towards the player when in range. However, when i try colliding with the enemy, the character controller (player) goes right through the enemy as if there is no collider on the enemy. I have a feeling the transform.Translate is causing this problem. Is their a better way of moving the enemy toward the player, or a way to fix this collision problem?
var lookAtTarget : Transform;
var runningspeed : int;
var range : int;
var damp : float;
var disable = true;
function Awake()
{
if (!lookAtTarget)
{
lookAtTarget = GameObject.FindWithTag("Player").transform;
ENEMY_HEALTH = 10;
}
}
function Update()
{
if(lookAtTarget && CanAttackTarget())
{
var rotate = Quaternion.LookRotation(lookAtTarget.transform.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
transform.Translate (0,0,runningspeed*Time.deltaTime);
//transform.position += transform.forward*runningspeed*Time.deltaTime;
}
}
function CanAttackTarget()
{
//check if in range
if(Vector3.Distance(transform.position, lookAtTarget.position) > range || Vector3.Distance(transform.position, lookAtTarget.position) < 2 )
{
return false;
}
var hit : RaycastHit;
if(Physics.Linecast(transform.position, lookAtTarget.position, hit))
{
if(hit.collider.gameObject.tag == "Player")
{
//Debug.Log("iseeu");
return true;
}
else
{
//Debug.Log("I see ");
return false;
}
//Debug.DrawRay(transform.position, transform.position-lookAtTarget.position t, Color.blue);
}
return true;
}
function OnCollisionEnter (hit : Collision)
{
if(hit.gameObject.tag == "Player")
{
Debug.Log("HITHIT");
}
}
Your player is fine. You should try adding a character controller to your enemy. Then you need to get the CC from your enemy to be able to move him around.
var target : Transform; //the p`layer
var enemy : Transform; //the enemy
var speed : float = 10; // well...the speed
function Update(){
enemy.LookAt(target); // the NPC looks at the player
// Here you access the Character Controller component and move your NPC with SimpleMove() giving the speed and the direction(forward).
//As the NPC is looking at you, forward is in your direction.
enemy.GetComponent(CharacterController).SimpleMove(speed*follower.forward);
animation.Play("Walk"); //You play the animation if you have one
}
}
Now they should naturaly collide just like your character
I found the solution thanks to everyones advice/help. Here is the fixed script incase anyone was having the same problem. Instead of puting a box collider on my enemy, i just added a character controller and attached this script:
var lookAtTarget : Transform;
var runningspeed : int;
var range : int;
var damp : float;
var disable = true;
function Awake()
{
if (!lookAtTarget)
{
lookAtTarget = GameObject.FindWithTag("Player").transform;
ENEMY_HEALTH = 10;
}
}
function Update()
{
if(lookAtTarget && CanAttackTarget())
{
GetComponent(CharacterController).SimpleMove(runningspeed*transform.forward);
}
}
function CanAttackTarget(){
if(Vector3.Distance(transform.position, lookAtTarget.position) > range || Vector3.Distance(transform.position, lookAtTarget.position) < 2 )
{
return false;
}
var hit : RaycastHit;
if(Physics.Linecast(transform.position, lookAtTarget.position, hit))
{
if(hit.collider.gameObject.tag == "Player")
{
GetComponent(SmoothLookAt).enabled = true;
return true;
}
else
{
GetComponent(SmoothLookAt).enabled = false;
return false;
}
}
return true;
}
function OnCollisionEnter (hit : Collision){
if(hit.gameObject.tag == "Player")
{
Debug.Log("HITHIT");
}
}