Search Unity

Resolved Cinemachine OnTargetObjectWarp method not working

Discussion in 'Cinemachine' started by GradientOGames, Jul 1, 2022.

  1. GradientOGames

    GradientOGames

    Joined:
    Feb 8, 2021
    Posts:
    28
    the method just doesn't work, I've calculated correct delta, i've tried switching between fixed update and late update in vcam (either way not gonna do that cause it makes camera choppy) and I've tried everything that I can think of
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using Cinemachine;
    4.  
    5. public class PlayerMain : MonoBehaviour
    6. {  
    7.     public Rigidbody2D PlayerRigidBody;
    8.     public Vector3 WorldSpawnPoint, PlayerSpawnPoint;
    9.     public GameObject PlayerTextureObject, PlayerMainObject;
    10.     public CinemachineVirtualCamera CameraSmoother;
    11.  
    12.     private void Start()
    13.     {
    14.         PlayerSpawnPoint = WorldSpawnPoint;
    15.     }
    16.  
    17.     private void OnTriggerEnter2D(Collider2D collision)
    18.     {
    19.         Debug.Log("E");
    20.         Debug.Log(collision.gameObject.layer);
    21.         if(collision.gameObject.layer == 3)
    22.         {
    23.             StartCoroutine(Die(1));
    24.         }
    25.         if (collision.gameObject.layer == 8)
    26.         {
    27.             PlayerSpawnPoint = collision.transform.position;
    28.         }
    29.     }
    30.  
    31.     IEnumerator Die(int DeathType)
    32.     {
    33.         Debug.Log("Die");
    34.         if (DeathType == 1)
    35.         {
    36.             PlayerTextureObject.AddComponent<Rigidbody2D>();
    37.             PlayerTextureObject.GetComponent<Rigidbody2D>().velocity = PlayerRigidBody.velocity;
    38.             PlayerTextureObject.GetComponent<Rigidbody2D>().angularVelocity = PlayerRigidBody.angularVelocity;
    39.             PlayerRigidBody.velocity = new Vector3(0, 0, 0);
    40.             PlayerRigidBody.angularVelocity = 0;
    41.             PlayerRigidBody.simulated = false;
    42.             yield return new WaitForSeconds(2);
    43.             PlayerMainObject.transform.localRotation = new Quaternion(0, 0, 0, 0);
    44.             Destroy(PlayerTextureObject.GetComponent<Rigidbody2D>());
    45.             PlayerTextureObject.transform.localPosition = new Vector3(0, 0, 0);
    46.             PlayerTextureObject.transform.localRotation = new Quaternion(0, 0, 0, 0);
    47.             Vector3 Delta = (PlayerMainObject.transform.position - PlayerSpawnPoint);
    48.             Debug.Log(PlayerMainObject.transform.position + "\n" + PlayerSpawnPoint + "\n" + Delta);
    49.             PlayerMainObject.transform.position = PlayerSpawnPoint;
    50.             CameraSmoother.OnTargetObjectWarped(PlayerMainObject.transform, Delta);
    51.             PlayerRigidBody.simulated = true;
    52.             yield return null;
    53.         }
    54.     }
    55. }
    56.  
    What I'm trying to do: when player dies (game is platformer), the player is teleported to checkpoint (or world spawn) and it causes a popping of the camera. After searching for a bit I found out that OnTargetObjectWarped is the way to go, so i added it in and it just no work. I think it is something to do with the method being in a courotine but I don't know. Can somebody please point me in the right direction.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,711
    You've got the delta backwards. Try this:
    Code (CSharp):
    1. Vector3 Delta = (PlayerSpawnPoint - PlayerMainObject.transform.position);
     
  3. GradientOGames

    GradientOGames

    Joined:
    Feb 8, 2021
    Posts:
    28
    TYSM it works! This had been plaguing all my games until now!
     
    Gregoryl likes this.