- Home /
Enemy behavior help
So my enemy behavior is working correctly, but my problem is the enemy doesn't have collision or gravity. I have tried adding colliders and rigidbody physics to the enemy, but no matter what she still doesn't have gravity or collision. I have also tried using SimpleMove instead of transform but with that, the enemy just stayed in place.
I can collide with her with the script disabled.
Here is my script:
using UnityEngine;
using System.Collections;
public class EnemyBehavior : MonoBehaviour
{
public enum EnemyState{IDLE = 0, WALK = 1, RUN = 2, ATTACK = 3, KILL = 4};
public AnimationClip enemyIdle;//setting animations
public AnimationClip enemyWalk;//walking
public AnimationClip enemyRun;//running/chasing
public AnimationClip enemyAttack;//attacking player
public AnimationClip enemyKill;//killing player
public Transform Player;//adds the player to the script
private int Stopped = 0;
public int WalkingSpeed = 1;//sets the normal walking speed
public int RunningSpeed = 5;//sets the running (chase player) speed
public int DefaultRunningSpeed = 5;
public int MaxDist= 8;//maximum distance the player can be before enemy stops chasing the player
public int MinDist= 1;//how close the player can be to enemy for enemy to start chasing player
public EnemyState currentState;
private bool PlayerDetected;
void Update()
{
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)//if that object has the tag "Player"
{
PlayerDetected = true;
if (PlayerDetected)
{
print ("Player Detected.");
transform.LookAt(Player);//Looks at the player
currentState = EnemyState.RUN;
transform.position += transform.forward*RunningSpeed*Time.deltaTime;//follows the player
}
}else if(Vector3.Distance(transform.position,Player.position) > MaxDist)
{
RoamAround ();
}
if (Vector3.Distance (transform.position,Player.position) < MinDist)
{
currentState = EnemyState.ATTACK;
Attack ();
}
if (currentState == EnemyState.IDLE)
{
//print ("The enemy is idle.");
animation.CrossFade (enemyIdle.name);
}
else if (currentState == EnemyState.WALK)
{
print ("The enemy is walking.");
animation.CrossFade (enemyWalk.name);
}
else if (currentState == EnemyState.RUN)
{
print ("The enemy is running.");
print ("Distance: " + Vector3.Distance(transform.position,Player.position));
animation.CrossFade (enemyRun.name);
RunningSpeed = DefaultRunningSpeed;
}
else if (currentState == EnemyState.ATTACK)
{
print ("The enemy is attacking.");
animation.CrossFade (enemyAttack.name);
RunningSpeed = Stopped;
}
else if (currentState == EnemyState.KILL)
{;
print ("The enemy is killing.");
animation.CrossFade (enemyKill.name);
}
}
void RoamAround()
{
currentState = EnemyState.IDLE;//Add other RoamAround stuff here
}
public void Attack()
{
//Add attacking stuff here
//Application.LoadLevel ("GameOver");
}
}
What can I do to this script to fix this problem?
Answer by Kiwasi · Jun 10, 2014 at 12:37 AM
Setting transform.position will always override physics settings.
To use physics you need
RigidBody.AddForce
RigidBody.MovePosition
RigidBody.Velocity
Or use animations to move your character and check the animate physics button.
Your answer
Follow this Question
Related Questions
My enemies stack and do not use gravity 2 Answers
Can't get gravity to work proberly (transform) 2 Answers
Collision / Gravity Problem Not Updating and Pass through 1 Answer
how to I zero out a parented child's position in X,Y,Z on collision? 1 Answer
How to move objects so that they bounce when something hits them? 1 Answer