Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Rotate camera along an axis to not be obstructed

Discussion in 'Cinemachine' started by Digital_Seven, Aug 18, 2020.

  1. Digital_Seven

    Digital_Seven

    Joined:
    Aug 20, 2015
    Posts:
    15
    I've got my camera all set up the way I want it to follow the character around. sometimes the character can go behind walls. I could use a shader to make it glow, or stencil, or something. But I'd like to try and tilt the camera a few degrees to have a better view of the target.

    I couldn't find anything out of the box for this. What I've tried is to write a script that would do a line cast between my character and my camera. If there was something there on the obstruction layer then I would try to Lerp the camera's rotation to have an unobstructed view.

    That for the most part worked. I would just find the preferred rotation to have a view of the character and then assign it using the transform.rotation. While this works it's got some weirdness. Like the camera will bounce around when trying to get a better view. I'm currently under the impression this is so because after rotating the camera the camera's transform.position changes and then the bouncing is hard to avoid.

    I'm trying to keep the camera at a rotation of 70,0,0 but the max would be 85,0,0 if the character was obstructed. Is there something I'm missing that would allow this? I'm currently only using the FollowTarget and a Framing Transposer. Or just some ideas on how to go about achieving the desired effect.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,153
    In general you are doing it the right way. Can you describe "bouncing around" in a little more detail? I don't really understand what you mean. The Framing Transposer algorithm will start with the vcam's rotation, then move the vcam so that it's in the desired relationship with the target, while keeping the rotation fixed.

    What version of CM are you using?
    Can you show an image of the vcam inspector?
     
  3. Digital_Seven

    Digital_Seven

    Joined:
    Aug 20, 2015
    Posts:
    15
    Hey there. Sorry for the delay. Been on a tools task the last couple of days.

    Cam version: 2.6.0

    Here is a gif of the jitter. Red means it's obstructed, Blue means it's got a view.
    Camera gitter.gif

    Here are the VCam settings:
    VCam Settings.JPG

    And because I believe this is most likely a logic issue, here is the relevant code snippets.
    Code (CSharp):
    1. void Update()
    2. {
    3.     if (TargetCharacter == null) return;
    4.     var targetPos = TargetCharacter.transform.position;
    5.     var speed = Speed * Time.deltaTime;
    6.     if (Physics.Linecast(transform.position, targetPos, ObstructionMask))
    7.     {
    8.         Debug.DrawLine(transform.position, targetPos, Color.red);
    9.         RotateToward(MaxRotation, speed);
    10.     }
    11.     else
    12.     {
    13.         Debug.DrawLine(transform.position, targetPos, Color.blue);
    14.         if (!IsPositionObstructed(PreferredRotation, speed))
    15.         {
    16.             RotateToward(PreferredRotation, speed);
    17.         }
    18.     }
    19. }
    20. private bool IsPositionObstructed(Vector3 targetPosition, float speed)
    21. {
    22.     var t = Vector3.Lerp(transform.rotation.eulerAngles, targetPosition, speed);
    23.     Debug.DrawLine(transform.position, targetPosition, Color.yellow);
    24.     return Physics.Linecast(transform.position, t, ObstructionMask);
    25. }
    26. private void RotateToward(Vector3 targetRotation, float speed)
    27. {
    28.     var q = Vector3.Lerp(transform.rotation.eulerAngles, targetRotation, speed);
    29.     var a = q - transform.rotation.eulerAngles;
    30.     if (a.magnitude > Deadzone)
    31.     {
    32.         transform.rotation = Quaternion.Euler(q);
    33.     }
    34. }
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,153
    I still don't quite understand what you mean by "bouncing around".
    Does it bounce around if you turn damping to 0?

    If that stops the bouncing, try checking this option:

    upload_2020-8-27_8-15-15.png