I’m making a 2D platformer , but i have a problem making the enemy face the player. I was able to make the main character face left or right by rotating its plane mesh 180 so its in reverse depending on what key is pressed a,or d…I want the toaster/ enemy to face the player if the its within a certain distance. In other word rotate the toaster to (90,0,0) on the xyz axis if the character is on the Right side of the toaster , and (90,180,0) if its on the left of the toaster. …
[20181-screenshot+(17).png|20181]
[20180-screenshot+(16).png|20180]
I made this script to check the distance between the Enemy and player and depending on the y value of the rotation it will flip to 180 or 0 on the y ,
the thing is that it doesnt work properly what am I doing wrong ? How do i make the Toaster face the player?
var NoticeDistance = 10;
var Player: Transform;
var RotatedToRight= false;
var RotatedToLeft= true;
function LateUpdate(){
var relativePoint = transform.InverseTransformPoint(Player.position);
if(Vector3.Distance(transform.position,Player.position) <= NoticeDistance){
if (relativePoint.x < 0.0 && Input.GetKeyUp("d") ){
print ("Object is to the Right");
transform.eulerAngles = Vector3(90,0,0);
/* if(transform.rotation.eulerAngles.y >= 160 ){
print("Y rotation is about 180");
RotateRight();
}*/
}
else if (relativePoint.x > 0.0 && Input.GetKeyUp("a") ){
print ("Object is to the Left");
transform.eulerAngles = Vector3(90,180,0);
/* if(transform.rotation.eulerAngles.y <= 5 ){
print("Y rotation is about 0");
RotateLeft();
} */
}
else{
print ("Object is directly ahead");
}
}
}
function RotateLeft(){
// yield WaitForSeconds (3); //Debug yeild
transform.eulerAngles = Vector3(90,180,0);
RotatedToLeft = true;
// yield WaitForSeconds (5);//Debug yeild
RotatedToRight = false;
}
function RotateRight(){
// yield WaitForSeconds (3);//Debug yeild
transform.eulerAngles = Vector3(90,0,0);
RotatedToRight = true;
// yield WaitForSeconds(5);// Debug yeild
RotatedToLeft = false;
}