- Home /
Rotation acting weird
As you can see in the gif, the rotation for my player entity is fine, but my enemy is acting weird. They share the same script, which I had written for a previous project with no issue. I don't get why it isn't working right here. Given that the script works fine everywhere else it's implemented, I thought there might be some issue with the gameobject, but I haven't found anything there that would cause this behavior. I appreciate any insight into the matter.
private C1_PlayerController player;
// Use this for initialization
void Start () {
player = FindObjectOfType<C1_PlayerController>();
}
// Update is called once per frame
void Update () {
Quaternion rot = Quaternion.LookRotation(transform.position - player.transform.position,Vector3.forward);
transform.rotation = rot;
transform.eulerAngles = new Vector3(0, 0, transform.eulerAngles.z);
}
what is the transform orientation for both enemy and player marker? is forward the actual direction they are looking in?
It is. I threw a bunch of Debug statements in and I found that when I'm to either the left or right of the enemy sprite, moving the player along the y axis any distance only yields a rotational shift of 2 or 3 degrees total.
Answer by DrummerB · Jun 26, 2018 at 03:39 PM
Try this:
void Update () {
transform.right = player.transform.position - transform.position;
// or transform.up, depending on which direction the arrow is pointing by default
}
The simplicity of this solution is infuriating. You, sir, are a godsend. I've been banging my head against this for two days.
Holy . I thought I kinda know the basic tricks to handle transforms but i seriously did not know that one could directly set the rotations by defining the transforms global directions...
Well done!