- Home /
Question by
chiako · Feb 23, 2013 at 06:54 AM ·
fpsfollow3rd person controller
How to make the 3rd person controller an enemy to my fps?
what should i do to make the 3rd person controller follow my fps? I have this code attatched to my 3rd person controller but it just run away and disappear and it doesn't follow my fps
var MoveSpeed : float = 2;
var Player : Transform;
var MinDist = 10;
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
this.animation.Play("idle");
//transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
else if(Vector3.Distance(transform.position,Player.position) <= MinDist){
this.animation.Play("walk");
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
}
}
Comment
Best Answer
Answer by deltamish · Feb 23, 2013 at 07:30 AM
Hi,Use this to make him follow you
var MoveSpeed : float = 2;
var Player : Transform;
var MinDist = 10;
private var tmpmovespeed:float;
public var leasmindistance:float = 2;
function Start(){
tmpmovespeed = MoveSpeed;
}
function Update(){
transform.LookAt(Player);//you can also use transform.rotation = Quaternion.Slerp....... it just as same only thing is it will be a bit more smooth
var direction = Player.position - transform.position;
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
this.animation.Play("idle");
}
else if(Vector3.Distance(transform.position,Player.position) <= MinDist){
MoveSpeed = tmpmovespeed;
this.animation.Play("walk");
transform.position = Vector3.Lerp(transform.position,Player.position,Time.deltaTime * MoveSpeed);
}
if(Vector3.Distance(transform.position,Player.position) <= leasmindistance){
MoveSpeed =0;
}
}
Note The above script is an alternative script
Hi check if your transform's local axis is different from global axis if yes then use this script
function Update()
{
transform.LookAt(Player);
//ommitted other
else if(Vector3.Distance(transform.position,Player.position) <= MinDist){
var dire:Vector3 = transform.TransformDirection(0,0,1);
this.animation.Play("walk");
transform.position += dire*MoveSpeed*Time.deltaTime;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613093230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help in Script (Java Script) 0 Answers
Camera rotation around player while following. 6 Answers
3D Text to follow FPS 1 Answer
Animate #rd person character 0 Answers
How do i get a weapon to follow the first person controller up and down? 0 Answers