Quaternion.lookrotation

Hey guys,

I am having problems with lock on feature. my player uses wasd for movement and the mouse for vertical and horizontal rotation

angleH += Mathf.Clamp (Input.GetAxis ("Mouse X"), -1, 1) * horizontalAimingSpeed * Time.deltaTime;
            angleV += Mathf.Clamp (Input.GetAxis ("Mouse Y"), -1, 1) * verticalAimingSpeed * Time.deltaTime;

void VerticalTurn(){
        if (ePressed) // moves the player up
            transform.Translate(Vector3.up * 1.0f * speed * Time.deltaTime);
        else if (qPressed) // moves the player down
            transform.Translate(Vector3.up * -1.0f * speed * Time.deltaTime);
        else if (ePressed && qPressed) { // supposed to do nothing but needs to be tested
        }
    }

i have a range of [-360,360] then it adds or subtracts as appropriate to kind of reset it otherwise it just keeps adding or subtracting. Not sure if that was necessary or relevant to my question, but just wanted to clarify.

Now when the player locks on i subtract player position from enemy position(essentially gives a direction), and then use quaternion.lookrotation to face the player towards the enemy while it is moving around it.

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);
            // rotates the player in the direction of the lock icon
            transform.rotation = Quaternion.Slerp(transform.rotation, lockRotation, turnSpeed* Time.deltaTime);;

My issue is when I disable lock on I want to stay looking in the direction the player is in while locked on and just move and rotate from there. I am missing some connection on how to keep angleV and angleH updated properly so that it will stay looking in that direction. Any advice or direction would be greatly appreciated. I’ve been working on it on and off for two days and this is driving me up the wall. Thank you in advance to anyone who replies

As a norm, I never use the Quaternion.LookRotation. I always like to do things very directly. Since Transform has a LookRotation, it basically says… Make me look here. I simply hold the current rotation, then Look at something, then Slerp/Lerp to the new rotation.

var rotation = transform.rotation;
transform.LookAt(lockIcon.transform.position);
transform.rotation = Quaternion.Slerp(rotation, transform.rotation, turnSpeed * Time.deltaTime);

same effect though it doesn’t stay in that angle it goes back to the angle you were looking in before enabling lock on

What are angleH and angleV? Which axes this angles belong to?
Or how do you compute the player rotation using these angles.
Because retrieving those angles depends on how you are using them.

they are computed with the mouse inputs angleH rotates the player with respect to the y axis and angle V rotates with respect to the x axis

In which order do you combine the rotations? Or are you using euler angles?

eular wow the first post had the wrong code for rotations here it is. I convert back to Quaternion before slerping. So both lockRotation and targetRotation are in the same format, but I don’t know how to update angleH and angleV during lock on to hold the rotation.

void Turn(){ // try to modify for a smoother rotation.
        // sets rotation based on mouse movements
        targetRotation = Quaternion.Euler (-angleV, angleH, 0);

        Debug.Log ("targetRotation = " + targetRotation + " when not locked on");
        // sets players rotation
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed* Time.deltaTime);
    }

You should be able to just read the euler representation back out from your look rotation. You may need to flip x as you do that with angleV.

I tried that I think. Could you maybe show an example of some sort so I can see it.

OK, in order to answer this question… I did another thread which should give me a basic camera.

http://forum.unity3d.com/threads/camera-simple-controller-from-scratch.410795/

Let me add some code to that, to help you out some.

using UnityEngine;
using System.Collections;
 
[RequireComponent(typeof(CharacterController))]
public class SimpleController : MonoBehaviour
{
    Camera camera; // the camera....
    float x, y; // handles mouse capture
    public float speed = 8; // for movement
    // for scrolling....
    public float minThirdPersonDistance = 0;
    public float maxThirdPersonDistance = 20;
    float thirdPersonDistance = 0;
    
    public Transform target;
    public float turnSpeed = 10;
 
 
    // Use this for initialization
    void Start()
    {
        // set the camera up
        camera = Camera.main;
        if (camera == null) camera = Camera.allCameras[0];
 
        // capture the camera's current euler angle.
        var euler = camera.transform.eulerAngles;
        x = euler.x;
        y = euler.y;
    }
 
    void Update()
    {
        if(target != null){
            // rotate smoothly to the target
            var rotation = transform.rotation;
            transform.LookAt(target);
            transform.rotation = Quaternion.Slerp(rotation, transform.rotation, turnSpeed * Time.deltaTime);
        } else {
            // capture the mouse for the view
            x -= Input.GetAxis("Mouse Y") * 20;
            y += Input.GetAxis("Mouse X") * 20;
     
            // clamp the X angle so that we dont look upside down.
            x = ClampAngle(x, -60, 60);
            camera.transform.position = transform.position + Vector3.up * 0.5f;
            camera.transform.eulerAngles = new Vector3(x, y, 0);
     
            // keep the camera looking at the mouse based angles
            var lookAt = camera.transform.TransformPoint(Vector3.forward);
            lookAt.y = transform.position.y;
            transform.LookAt(lookAt);
        }
        
 
        // handle movement
        var move = Vector3.zero;
        move.z = Input.GetAxis("Vertical");
        move.x = Input.GetAxis("Horizontal");
        if (move.sqrMagnitude > 1) move.Normalize();
        move *= speed;
        move = transform.TransformDirection(move);
        var controller = GetComponent<CharacterController>();
        controller.SimpleMove(move);
 
        // handle the third person camera
        thirdPersonDistance -= Input.GetAxis("Mouse ScrollWheel") * 20;
        thirdPersonDistance = Mathf.Clamp(thirdPersonDistance, minThirdPersonDistance, maxThirdPersonDistance);
        camera.transform.Translate(-Vector3.forward * thirdPersonDistance, Space.Self);
    }
 
    // clamp the current angle
    float ClampAngle(float angle, float min, float max) {
        angle %= 360;
        if (angle > 180) angle -= 360;
        return Mathf.Clamp(angle, min, max);
    }
}