Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Resolved Undesirable damping in custom extension

Discussion in 'Cinemachine' started by SergeyZh, Jun 14, 2020.

  1. SergeyZh

    SergeyZh

    Joined:
    Feb 1, 2016
    Posts:
    4
    I'm trying to create custom confiner for 2.5D sidescroller by casting a sphere around target character and applying hit distance as Position Correction. Here is a gist of my code:

    Code (CSharp):
    1. if (stage == CinemachineCore.Stage.Finalize) {
    2.     int overlapCount = Physics.OverlapSphereNonAlloc(resultPos,
    3.         boundingSphereRadius,
    4.         overlapBuffer,
    5.         boundingMask);
    6.  
    7.     for (int i = 0; i < overlapCount; i++) {
    8.         var overlapPoint = overlapBuffer[i].ClosestPoint(resultPos);
    9.         collisionFix -= (resultPos - overlapPoint).normalized
    10.             * ((resultPos - overlapPoint).magnitude - boundingSphereRadius);
    11.     }              
    12.  
    13.     state.PositionCorrection += collisionFix;
    14. }
    Problem is: Transposer X damping creates undesirable bevahior by dragging camera in opposite direction, while character is moving inside confined zone. Everything works as expected when damping is set to zero. Is there any way to ignore Body damping in Extension?
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    How do you calculate resultPos?
     
  3. SergeyZh

    SergeyZh

    Joined:
    Feb 1, 2016
    Posts:
    4
    var resultPos = vcam.Follow.position
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    So resultPos is the target position. How do you take into account the vcam position? state.PositionCorrection is a delta for the vcam position, so it's not clear to me what this code is trying to accomplish. Maybe you can explain that a little more fully.
     
  5. SergeyZh

    SergeyZh

    Joined:
    Feb 1, 2016
    Posts:
    4
    I'm trying to create a similar behavior to existing 2D confiner, but for perspective camera in 2.5D environment.
    General idea is to overlap a sphere around camera target with level boundaries or other obstacles and subtract delta from final camera position.

    Correct behavior with Transposer XDamping set to zero:


    Everything works as intendent, except when Transposer XDamping is set to anything other than zero:


    I would like to keep damping values in general cases, but ignore them, when confinement should be active.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,811
    My first thought is that you should be confining based on the vcam position, not the player position. Then damping won't interfere. Put the sphere at the vcam position + some Z amount (same as vcam's Z distance from player), and make your calculations based on that, instead of a sphere around the player.
     
  7. SergeyZh

    SergeyZh

    Joined:
    Feb 1, 2016
    Posts:
    4
    It actually worked! Thanks for your help.
     
    Gregoryl likes this.
unityunity