Search Unity

[SOLVED] Quaternion.LookRotation no X rotation somehow

Discussion in 'Scripting' started by WheresMommy, Mar 27, 2015.

  1. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Hey everyone,

    I am currently working on a game, where you can like lock a target, like in dark souls. everything is working fine except, my modified MouseOrbit.js is not targeting with the right X rotation. It seems like the rotation only works in Y axis, so it always looks straight forward to the target instead of the targets position.y:

    Code (CSharp):
    1. function LateUpdate () {
    2.     if (target) {
    3.         if (!lockCamera){
    4.             x += Input.GetAxis("HorizontalLook") * xSpeed * 0.2;
    5.             y -= Input.GetAxis("VerticalLook") * ySpeed * 0.2;
    6.            
    7.              y = ClampAngle(y, yMinLimit, yMaxLimit);
    8.            
    9.              var rotation = Quaternion.Euler(y, x, 0);
    10.             var position = rotation * Vector3(0.0, 1, -distance) + target.position;
    11.            
    12.             currentPos = position ;
    13.         } else {
    14.             rotation = Quaternion.LookRotation(rotateTarget.position - target.position, Vector3.up);
    15.                currentPos = camPos.position ;
    16.         }
    17.        
    18.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 20);
    19.         transform.position = Vector3.Slerp(transform.position, currentPos, Time.deltaTime * 20);
    20.     }
    21. }
    The rotateTarget position is the enemy, the target position is the player himself. The CamPos is a empty gameObject for the camera if it is locked.

    Any ideas, if I am using the lookRotation function in a wrong way or something else?
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Why are you substracting player's position from the enemy's position? If you want your camera to look at your enemy, you should be substracting camera's position from it. Simply doing this, just makes an object to look at the another one :

    Code (CSharp):
    1. Vector3 dir = objectToLookAt.position - transform.position;
    2.  
    3.         Quaternion q_L = Quaternion.LookRotation(dir, Vector3.up);
    4.  
    5.         transform.rotation = Quaternion.Slerp(transform.rotation, q_L, Time.deltaTime * 20);
     
    Last edited: Mar 27, 2015
    WheresMommy likes this.
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I guess, there was some weird things going on in my brain :D Too much code today. My code works perfectly. Also fixed my struggle with the resetting rotation when you leave the button, so the character is facing in the direction he was locking the camera at. If anyone needs this to play around in the mouseorbit.js and wants a similiar camera. Here is the code.

    Code (CSharp):
    1. function LateUpdate () {
    2.     if (target) {
    3.         if (!lockCamera){
    4.             x += Input.GetAxis("HorizontalLook") * xSpeed * 0.2;
    5.             y -= Input.GetAxis("VerticalLook") * ySpeed * 0.2;
    6.            
    7.              y = ClampAngle(y, yMinLimit, yMaxLimit);
    8.            
    9.              rotation = Quaternion.Euler(y, x, 0);
    10.             position = rotation * Vector3(0.0, 1, -distance) + target.position;
    11.  
    12.             currentPos = position ;
    13.         } else {
    14.             var dir = rotateTarget.position - transform.position;
    15.             rotation = Quaternion.LookRotation(dir, Vector3.up);
    16.            
    17.             x = rotation.eulerAngles.y ;
    18.             y = rotation.eulerAngles.x ;
    19.            
    20.                currentPos = camPos.position ;
    21.         }
    22.         transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * 20);
    23.         transform.position = Vector3.Slerp(transform.position, currentPos, Time.deltaTime * 20);
    24.     }
    25. }
    Thank you lineupthesky for that quick response and clearifying my head :D
     
    lineupthesky likes this.
  4. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    No problem, that kind of things happen sometimes it's a good thing to go out and have some fresh air from time to time :D. Good luck, cheers !