- Home /
Enemy Rotation Problem
Hey guys, I have a problem here. I have been trying to fix it but i just couldn't get it. I am still new in unity, started using unity about 7 weeks already. Okay, so i have this script of my enemy following my main character, and what i want it to do is to rotate as well, but it seems like the enemy is rotating but in a weird way. here is the code Thank you so much for helping.
var Player : Transform;
var MoveSpeed = 4;
var MaxDist = 100;
var MinDist = 50;
private var tr : int = 180;
private var moveDirection = Vector3.zero;
function Start ()
{
}
function Update ()
{
//transform.LookAt(Player);
var dist: float = Vector3.Distance(transform.position,Player.position);
var dir: Vector3 = Player.position - transform.position;
//Debug.Log(dir);
dir.Normalize();
if(dist > MinDist)
{
transform.position += dir * MoveSpeed * Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
transform.eulerAngles.y += (transform.eulerAngles.y - tr) / 2;
}
if(dir.x>0){
//looking to the right
tr = 180;
} else {
tr = -180;
}
// if(dir.x<0){
//looking to the left
//tr = 0;
//transform.eulerAngles.y += (transform.eulerAngles.y + tr) / 2;
}
}
I want to rotate the enemy to the player, which i have already tag the enemy to the player. It rotates weird is like, when the player is nearby the enemy and just keep pressing left and right the enemy will turn but it disappeared. @supernat
I see you commented out a 'transform.LookAt(Player);' Why did that not work for you. Is it because 1) it did not look at the player, or 2) it was an immediate rotation and you wanted to rotate over time, or 3) it rotated on more than one axis and you only wanted to rotate on the 'y' axis? The fix will depend on the reason.
@robertbu It did look at the player when I put the 'transform.LookAt(Player);' just that it rotate everywhere. I only want it to rotate on X axis. Like when the player turn right it will snap on right as well
@robertbu Sorry I didn't mention it was 2D now it doesn't do the weird turning, but it still doesnt look at where the player is looking at :(
http://s2.postimg.org/7wfcnusd5/Untitled.jpg
@Pyrian Do i need to rotate my whole scene?
Answer by robertbu · May 23, 2014 at 05:55 AM
You say, "I only want it to rotate on the X axis," but your existing code is rotating the object on the y axis. So I'm going to assume you really want a 'y' axis rotation. If so, this is likely what you are looking for:
function Update () {
var lookPos = Player.position;
lookPos.y = transform.position.y;
transform.LookAt(lookPos);
var dist: float = Vector3.Distance(transform.position,Player.position);
var dir: Vector3 = Player.position - transform.position;
dir.Normalize();
if(dist > MinDist) {
transform.position += dir * MoveSpeed * Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist) {
transform.eulerAngles.y += (transform.eulerAngles.y - tr) / 2;
}
}
}
It works by putting the Players position on the same 'y' as this object, resulting in a rotation only on the y axis.
@robertbu the enemy become like this and it doesn't rotate to where my player is facing
I did not catch it was 2D. I'm not sure of all of your expected behavior. Here is a rewrite for 2D:
#pragma strict
var Player : Transform;
var $$anonymous$$inDist : float;
var $$anonymous$$oveSpeed : float;
var $$anonymous$$axDist : float;
function Update () {
var dist: float = Vector3.Distance(transform.position,Player.position);
var dir: Vector3 = Player.position - transform.position;
dir.Normalize();
if(dist > $$anonymous$$inDist) {
transform.position += dir * $$anonymous$$oveSpeed * Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= $$anonymous$$axDist) {
var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
}
}
Note the rotation is done by these two lines:
var angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
@robertbu Sorry i didnt mention it was 2D in the first place, ermm it doesn't turn weird anymore, but now the enemy is not looking where the player is looking
The above code assumes that the side that is considered the front side is on the right. If the side is up, you need to add 90 to 'angle' before calling AngleAxis().
Your answer
Follow this Question
Related Questions
2D enemy AI 1 Answer
How to cover buttons with game objects? 1 Answer
Reverse object position order 1 Answer
can't detect what player collides with, tried everything and all other posts... 2 Answers