0
\$\begingroup\$

Issue

I have 2 scripts on my camera, one for following the player ( FollowTarget.cs ), and one which is supposed to, over time, Slerp the camera's transform position and rotation around the player on death ( DeathCam.cs ).

With DeathCam.cs I am trying to get a nice smooth movement to the [desired/set in inspector] position and rotation values over time on death but currently the camera jumps to straight to the position and rotation set in inspector. Which I think maybe down to the transform.position = camPos; in Update.

I have tried to set the position and rotation in the PlayDeathCam() function but that fails to work and is currently commented out and replaced by a bool which checks in Update to see if the death cam is active. I also don't want to use an animator to do the job as there are other issues this would create.


Code: FollowTarget.cs

        using System.Collections;
        using System.Collections.Generic;
        using UnityEngine;
        
        public class FollowTarget : MonoBehaviour{
        
          [Header("----- Target To Follow ---------------------------------------------")]
          public GameObject target;
        
          [Header("----- Adjustments To Make ------------------------------------------")]
          public Vector3 adjustVal;
        
           void Update(){
              pos = new Vector3( target.transform.position.x + adjustVal.x,
                                 target.transform.position.y + adjustVal.y,
                                 target.transform.position.z + adjustVal.z);
              
              transform.position = pos;
           }
         }

Code: DeathCam.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DeathCam : MonoBehaviour{

  [Header("----- Duration -----------------------------------------------------")]
  [SerializeField] float duration = 5.0f;

  [Header("----- Positions ----------------------------------------------------")]
  [SerializeField] float finalPosX;
  [SerializeField] float finalPosY;
  [SerializeField] float finalPosZ;


  [Header("----- Angles -------------------------------------------------------")]
  [SerializeField] float tiltAngleX;
  [SerializeField] float tiltAngleY;
  [SerializeField] float tiltAngleZ;

  [Header("--------------------------------------------------------------------")]
  [SerializeField] Vector3 camPos;
  [SerializeField] Quaternion camRot;

  [SerializeField] bool deathCamActive;


///[ Start]
    void Start(){

    }


///[ Update]
    void Update(){
      if (deathCamActive) {
        camPos = new Vector3((transform.position.x + finalPosX),
                             (transform.position.y + finalPosY),
                             (transform.position.z + finalPosZ));
                             
        transform.position = camPos;
      }
    }


///[ PlayDeathCam]
    public void PlayDeathCam(){
      float tiltAroundX = camPos.x * tiltAngleX;
      float tiltAroundY = camPos.y * tiltAngleY;
      float tiltAroundZ = camPos.z * tiltAngleZ;

      // camPos = new Vector3((transform.position.x + finalPosX),
      //                     (transform.position.y + finalPosY),
      //                     (transform.position.z + finalPosZ));

      camRot = Quaternion.Euler(tiltAroundX, tiltAroundY, tiltAroundZ);

      transform.position = Vector3.Slerp(transform.position, camPos,  Time.deltaTime * duration);
      deathCamActive = true;

      transform.rotation = Quaternion.Slerp(transform.rotation, camRot,  Time.deltaTime * duration);

      StartCoroutine("DeathCamSlow");
    }


///[ DeathCamSlow]
    IEnumerator DeathCamSlow(){
      Time.timeScale = 0.3f;
      yield return new WaitForSeconds(1.6f);
      Time.timeScale = 1.0f;
    }

}
\$\endgroup\$
1
  • \$\begingroup\$ I don't see any mechanism to make FollowTarget.cs stop working. Maybe this is causing the problem? \$\endgroup\$
    – Mangata
    May 6 at 14:17

0

You must log in to answer this question.

Browse other questions tagged .