- Home /
How to make enemy slowly run to player
I make a horror game and my ghost model is fastly run to player. I want it to run slowly or run slowly and stop nearest at the player screen or anyway to make it horror. here is my code.
var distance;
var target : Transform;
var lookAtDistance = 15.0;
var attackRange = 10.0;
var moveSpeed = 0.5;
var damping = 6.0;
private var isItAttacking = false;
function Update ()
{
distance = Vector3.Distance(target.position,
transform.position);
} 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); }
function OnTriggerEnter (hitInfo : Collider)
{
Debug.Log("I'm in Collider Trigger:
"+hitInfo.gameObject.name);
if(hitInfo.gameObject.name=="PlayerTrigger") { Destroy(GameObject.Find("Ghost New 1")); } }
You use quotation in marking your code. Please edit your question, delete the code, past in a new copy, and use the 101/010 button to format the code. With code this unreadable, far fewer people with make any attempt to help you.
Answer by Levocakes · Feb 27, 2014 at 08:33 PM
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); }
If you are trying to make this enemy move slowing you can edit the moveSpeed
variable you have defined and the damping
also. Try different values to get it to the speed you want. If you want it to move slower set smaller values a for the MoveSpeed
. In addition to changing the movement speed, you can change the damping
value to make it turn slower when it is closer to you.