- Home /
Enemy object model not facing the player correctly
I created an enemy object which has a model and wrote a script that makes the enemy face the player when the player is close. However, when I get close the model of the enemy is rotating wrong, i.e. it should be rotated 90 degrees to make the model face the player correctly. Here is a pic of the problem:
You can see how the model is facing the player sideways (and its like that all the time when following the player, I tried to somehow add 90 degrees to the enemy object and make the model face the player correctly but I don't know where to modify that or how. The code for rotation is this:
private void Update() { if (Vector3.Distance(igrac.position, this.transform.position) < 10) { Vector3 direction = (igrac.position - this.transform.position); direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(direction), 0.1f);
}
is it because the front direction for your enemy is wrong? The front of your enemy should be facing on local Z-axis
Answer by HawkSandwich · Jun 16, 2017 at 08:03 AM
Heya :)
There are a few ways of doing this. One is to add 90 degrees on the next frame. It might look something like this:
this.transform.rotation = Quaternion.Slerp (this.transform.rotation, Quaternion.LookRotation(direction), .1f);
this.transform.Rotate(0, 90, 0); //this value might be on the wrong axis, or maybe it needs to be negative instead.
Of course, this isn't the most efficient way of solving the problem, but it's probably the simplest to understand.
Another way of doing this might be something like this, although I haven't tested it so it could be completely wrong.
this.transform.rtation = Quaternion.Slerp (this.transform.rotation + Quaternion.Euler(0, 90, 0), Quaternion.LookRotation(direction), 0.1f);
I hope I helped :)
OMG YOU JUST SAVED MY LIFE I HAVE BEEN LOOKING EVERYWHERE FOR THE ANSWER THANK YOU SO MUCH
Your answer
Follow this Question
Related Questions
Everything working fine in Editor but in device the find object with tag throwing null exception 2 Answers
How can I copy all scripts from one gameobject to another in code? 1 Answer
Can I make Money collecting script without attaching it to a object ? I tried but I got error 2 Answers
How to attach a (that world type) canvas to a moving player? 0 Answers
Reference DLL's 1 Answer