Search Unity

Target Group doesn't work with OnTargetObjectWarped

Discussion in 'Cinemachine' started by Devastadus, Oct 17, 2019.

  1. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    80
    Hello im making a 2D space shooter. and the players can warp to another side of the map. I have a virtual camera Following a target group containing the players.

    When the target group warp the OnTargetObjectWarped isn't working properly. I've notice it does work in a demo project where it will just follow a gameobject but doesn't work when following a target group. Please help.


    Code (CSharp):
    1.  
    2. private void OnTriggerExit2D(Collider2D other) {
    3.     if (other.name.Equals("PlayerArena")) {
    4.         Vector3 newPosition = Utilities.Vector2OppositeSideOfSquare(other.transform, innerArenaBoxCollider2D, 1.5f);
    5.         Vector3 movementDelta = newPosition - other.transform.position);
    6.         other.transform.position = newPosition;
    7.         MoveObjectsInArena(playersHolderTransform);
    8.         virtualCamera.OnTargetObjectWarped(playersGroup, movementDelta);
    9.         if (OnPlayerArenaTeleport != null)
    10.             OnPlayerArenaTeleport();
    11.     }
    12. }
    13.  
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    The group's position gets updated, based on the positions of its members, in the group's Update() or LateUpdate(), depending on what you've selected. Is it possible that you're calling OnTargetObjectWarped() before the group has updated its position? Try moving the group also in MoveObjectsInArena().
     
    Devastadus likes this.
  3. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    80
    Thank you so much for helping me. I was using fixed update for my cinemachine brain vcam update method. Changing it to smart update or late update fixes it but the camera moves choppy. I'm assuming the fixed update is being called before the OnTargetObjectWarped() is called which is being called from theOnTriggerExit2D(). I'll do more digging to fix this new issue
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    The group updates its own position, and has its own update method setting, The brain is not involved here. Looking at your code, there is no opportuniity for the group to update, since you are moving the objects then immediately calling OnTargetObjectWarped().
     
  5. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    80
    I am new at this. How would i update the target group? I don't see an API call method in target group to update it.
     
  6. Devastadus

    Devastadus

    Joined:
    Jan 27, 2015
    Posts:
    80
    @Gregoryl So I've been playing around OnTargetObjectWarp and it still doesn't seem like it works when your using a target group. I have the target group set update method on Update. So i get the old frame, wait for it to render. i can see in debug.log() the position changes. but when i call the OnTargetObjectWarped() it still doesn't work. I don't know what to do at this point besides scrapping cinemachine and going back to the old way

    Code (CSharp):
    1.  
    2. private void OnTriggerExit2D(Collider2D other) {
    3.     if (other.name.Equals("PlayerArena")) {
    4.        
    5.         Vector3 newPosition = Utilities.Vector2OppositeSideOfSquare(other.transform, innerArenaBoxCollider2D, 1.5f);
    6.         other.transform.position = newPosition; // Warps my GameObjects
    7.         MoveObjectsInArena(playersHolderTransform);// Warps my GameObjects
    8.        
    9.         StartCoroutine(WaitForMoveCamera());
    10.     }
    11. }
    12.  
    13. IEnumerator WaitForMoveCamera() {
    14.     Vector3 oldPosition = playersGroup.transform.position; //Get old position of target group
    15.     Debug.Log(oldPosition);
    16.     yield return new WaitForEndOfFrame();
    17.     Vector3 newPosition = playersGroup.transform.position; //Gets the new position after it updates
    18.     Debug.Log(newPosition);
    19.     virtualCamera.OnTargetObjectWarped(playersGroup, oldPosition - newPosition);
    20. }
    21.  
     

    Attached Files:

  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,725
    That approach should work. I think maybe it's just that your delta is wrong. Try this instead:
    Code (CSharp):
    1. virtualCamera.OnTargetObjectWarped(playersGroup, newPosition - oldPosition);
     
    Devastadus likes this.