Lock on with euler angles

Hey guys,

i’m trying to finish up this lock on function and I have made some decent headway. Only thing I need left is to keep the player facing the lock on direction when lock on is disabled

if (lockOn) {
            // right now finds closest enemy of everyone in scene want to modify to have a range
            closestEnemy = FindClosestEnemy ();
            // sets lock icon to closest enemy to player
            lockIcon.transform.position = new Vector3(closestEnemy.transform.position.x, closestEnemy.transform.position.y, closestEnemy.transform.position.z);
            lockIcon.SetActive (true); // turns the object connected to lockIcon on
            // direction of lock on
            Vector3 relPos = lockIcon.transform.position - transform.position;
            // Sets a rotation for the player to look in
            lockRotation = Quaternion.LookRotation (relPos, Vector3.up);
            //Debug.Log ("targetRotation = " + targetRotation + " when locked on");
            // rotates the player in the direction of the lock icon
            transform.rotation = Quaternion.Slerp(transform.rotation, lockRotation, turnSpeed* Time.deltaTime);;
            angleH = transform.eulerAngles.y;
            angleV = transform.eulerAngles.x - 310;
            dummyRotation = Quaternion.Euler (-angleV, angleH, 0);
            Debug.Log("dummyRotation = " + dummyRotation);
            Debug.Log("lockRotation = " + lockRotation);
            Debug.Log ("angleV = " + angleV + " and angleH = " + angleH + " while locked on");
            turnInput = Input.GetAxis ("Horizontal"); // turns player left or right in place
            checkKeystrokes ();
            // method that moves player left and right
            Strafe ();

        }

Above is the lock on function. relPos is the direction the player needs to look in to look at the locked on target. then i slerp lockRotation after the LookRotation function. All of this works properly. angleH and angleV are used when not locked on to track mouse movements to rotate the player; which is why I need to keep them updated as the player is locke don so that when the player disables lock on it stays looking in that direction. The dummyRotation uses the same method I use to handle normal rotations so I could compare it to the lockRotation. The problem I am running into is that when comparing dummyRotation and lockRotation the y and w parameters (these are quaternions) are always opposite sign. What I figured out is that I can always get to the desired rotation by manually adjusting angleV, but I don’t know if there is a method to use where it will be automatic.

dummyRotation = (-0.2, -0.3, 0.1, -0.9)
lockRotation = (-0.2, 0.3, 0.1, 0.9)

Just an example of what I mean with the opposite sign. No matter how close or where I am oriented around the enemy the x/z values are correct and the y/w values are wrong, but from one place to the next the adjustment of angleV is different. Any help would be awesome!!! Thank you in advance!!

pay close attention…

dummyRotation = Quaternion.Euler (-angleV, angleH, 0);

does exactly what you are saying it does…

Why are you using -angleV?

cause if it isn’t negative it inverts rotating up and down

why would this not work?

angleH = transform.eulerAngles.y;
angleV = transform.eulerAngles.x;
dummyRotation = Quaternion.Euler (angleV, angleH, 0);

dummyRotation = (0.2, -0.3, -0.1, -0.9)
lockRotation = (-0.2, 0.3, 0.1, 0.9)

That is what happens if anglev isn’t negative. If i inverse the dummy rotation i get

targetRotation = (-0.2, 0.3, 0.1, -0.9)

so still not really correct.

Also I changed it so I don’t have to have the negative angleV

Ok sooooo a little confused I guess the negative was throwing it off cause it is working great now. I swear I have actually tried what you said yesterday and it didn’t seem to work. Maybe I was just frustrated at that point anyways thank you for the help on multiple posts man!!