Search Unity

Cam not looking at target(s)

Discussion in 'Scripting' started by h0nka, Sep 19, 2017.

  1. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Hi there,

    I'm trying to make a very basic cam follow script. The thing is that the camera has to follow x-rotation of 1 transform and the y- and z-rotation of another. It currently does this, however, it does not also look at any of the targets (as it rotates in the same manner). My question is: How can I have the camera look at (and follow) the target(s) while also following the z-rotation of the yzTransform? Here is the code so far... BTW the two transforms are parent and child (yzTarget -> xTarget) at the same position so it does not matter which transform the camera looks at as long as the rotation follows the specifications above...

    The code so far:

    Code (CSharp):
    1. void Update () {
    2.  
    3.         //Vector3 targetPos = new Vector3(xTarget.position.x, yzTarget.position.y, yzTarget.position.z);
    4.  
    5.         Quaternion q = Quaternion.Euler(xTarget.eulerAngles.x, yzTarget.eulerAngles.y, yzTarget.eulerAngles.z);
    6.         transform.rotation = Quaternion.Slerp(transform.rotation, q, rotationSpeed * Time.deltaTime);
    7.  
    8.  
    9.         //transform.position += transform.forward * moveSpeed * Time.deltaTime;
    10.  
    11.         }
     
  2. RoMax92

    RoMax92

    Joined:
    May 29, 2017
    Posts:
    135
  3. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    I had a look at the video you suggested. It does however not really address the issue I am currently having. The problem with using transform.LookAt(target.position) is that it does not follow the local z-rotation of the transform controlling the rolling (z-rotation). I am therefore back to rotating the appropriate axes with the right angles but not looking at the transform(s) being controlled. Any suggestions as to how to achieve this?

    This is what I have now:

    Code (CSharp):
    1. void LateUpdate () {
    2.  
    3.         transform.position = targetYZ.position - offset;
    4.  
    5. //        transform.LookAt(targetYZ.position);
    6.  
    7.         transform.RotateAround(target.position, targetYZ.up, targetYZ.eulerAngles.y);
    8.         transform.RotateAround(target.position, targetX.right, targetX.eulerAngles.x);
    9.         transform.RotateAround(target.position, targetYZ.forward, targetYZ.eulerAngles.z);
    10.     }
     
  4. sadsack

    sadsack

    Joined:
    May 27, 2015
    Posts:
    156
    I think you have to change global xyz to local xyz. I had that problem a while back, can't remember what I did.
    I will go back and see if I can find the script.
     
    RoMax92 likes this.
  5. h0nka

    h0nka

    Joined:
    Apr 7, 2013
    Posts:
    109
    Thanks! That would be great.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I've never tried to adjust all 3 axis for a camera setup, so I can't be 100% sure this will work..

    However, for myself (and others who ask questions about 1/2 axis) - it's really helpful if you have a 3-part camera rig.
    For 2 axis, for example, the rig is an empty game object. It has an empty game object child, and that child has the Camera. When the x rotation is changed, just the top object adjusts to match, and for the y-rotation, it's the first child.
    This, plus some code to update it's position (and set values for offset on the game object in the scene), the camera and its rotation work out very nicely.
    I dunno if that's helpful for you. (maybe with 3 axis, you add another child that does the 'z'?) lol. Sorry I can't be more specific, because I've never tried it :)