- Home /
2.5D enemy-player tracking script does not work.
Because I don't know how to use the vector.dot thing, I made a roundabout method.
There is a secondary part of an enemy that watches the player, and will change the direction of it's parent depending on where they are. the direction is based upon it's own rotation.
if(transform.rotation.y >= 0){
if(transform.rotation.y < 45){
direction = 1;
}}
else if(transform.rotation.y >= 45){
if(transform.rotation.y < 135){
direction = 2;
}}
else if(transform.rotation.y >= 135){
if(transform.rotation.y < 225){
direction = 3;
}}
else if(transform.rotation.y >= 225){
if(transform.rotation.y < 315){
direction = 4;
}}
else if(transform.rotation.y >= 315){
if(transform.rotation.y < 360){
direction = 1;
}}
if(direction == 1){
parent.renderer.material.mainTexture = tex1;
}if(direction == 2){
parent.renderer.material.mainTexture = tex2;
}if(direction == 3){
parent.renderer.material.mainTexture = tex3;
}if(direction == 4){
parent.renderer.material.mainTexture = tex4;
}
No matter where the player goes, the direction Stays at 1, despite the transform's rotation in the inspector changing depending on where the player is.
Answer by Phantomized · Feb 14, 2015 at 09:36 PM
When you are using "else if" it will only enter that conditional statement if the preceding related "if" statement was false. Since "transform.rotation.y >= 0" is always true, all the "else if" statements will be ignored. You could instead use:
if (transform.eulerAngles.y >= 0 && transform.eulerAngles.y < 45) etcetera..
edit on my answer: Use transform.eulerAngles.y instead in this context, as that will return the angle in degrees.
tried changing it to
if(transform.rotation.y >= 0 && transform.rotation.y < 45){
direction = 1;
}else if(transform.rotation.y >= 45 && transform.rotation.y < 135){
direction = 2;
}else if(transform.rotation.y >= 135 && transform.rotation.y < 225){
direction = 3;
}else if(transform.rotation.y >= 225 && transform.rotation.y < 315){
direction = 4;
}else if(transform.rotation.y >= 315 && transform.rotation.y < 360){
direction = 1;
}
still stays at 1.
Ah! Sorry I didn't even think about the fact that you used transform.rotation. That function will return a value between 0 and 1 based on your rotation. Use transform.eulerAngles.y ins$$anonymous$$d to get them in degrees.
I should put the full scripts I'm using, because the eulerangles doesn't work either.
the updated script is as follows,
var daPlayer : GameObject;
var parent : GameObject;
var direction : int = 0;
var tex1 : Texture2D;
var tex2 : Texture2D;
var tex3 : Texture2D;
var tex4 : Texture2D;
function Start(){
daPlayer = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
}
function Update() {
if(transform.eulerAngles.y >= 0 && transform.eulerAngles.y < 45){
direction = 1;
}else if(transform.eulerAngles.y >= 45 && transform.eulerAngles.y < 135){
direction = 2;
}else if(transform.eulerAngles.y >= 135 && transform.eulerAngles.y < 225){
direction = 3;
}else if(transform.eulerAngles.y >= 225 && transform.eulerAngles.y < 315){
direction = 4;
}else if(transform.eulerAngles.y >= 315 && transform.eulerAngles.y < 360){
direction = 1;
} direction = 1;
if(direction == 1){
parent.renderer.material.mainTexture = tex1;
}else if(direction == 2){
parent.renderer.material.mainTexture = tex2;
}else if(direction == 3){
parent.renderer.material.mainTexture = tex3;
}else if(direction == 4){
parent.renderer.material.mainTexture = tex4;
}
}
and in the same object, I have this script,
var daPlayer : GameObject;
function Start(){
daPlayer = GameObject.FindGameObjectWithTag("$$anonymous$$ainCamera");
}
function Update () {
var lookPos = daPlayer.transform.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 100);
}
are the two scripts interfering with eachother? or is it another problem, because even with your suggestion, the direction stays at 1.
line 28 in the first script quote. You have an extra "direction = 1:" after the conditional statements. Try removing that.
didn't see that last one, it shouldn't have been there, thanks for pointing it out, because it works now.
Your answer

Follow this Question
Related Questions
Forward and back movements with a camera emulating an isometric view 1 Answer
Mouse axes based on position, not movement. 1 Answer
How Do i Prevent The First Person Camera From Turning 0 Answers
Camera rotates when I run into a wall 0 Answers
How to make the camera follow the player while still being able to be rotated? 1 Answer