Players camera spins occasionally when player dies

Silver100

Member
Hi,

My players camera spins occasionally when player dies. I have tried implenting solutions unparenting player/camera and freezing input on Isdead ideas that came to mind below but still not had an effect. Any idea on how to prevent this happening? Or prehaps how to lock the camera on isDead?


Code:
if (playerCurrentHealth < 0 && !isDead) //
                                      
        {

            {
                EventHandler.ExecuteEvent(Player, "OnEnableGameplayInput", false);// making sure input is disabled to avoid further movement
                var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();//Opsive API ref
                var dieAbility = characterLocomotion.GetAbility<Die>();
                // Tries to start the die ability. There are many cases where the ability may not start,
                // such as if it doesn't have a high enough priority or if CanStartAbility returns false.
                characterLocomotion.TryStartAbility(dieAbility);//Opsive API ref
                Player.transform.SetParent(null);
                Camera.transform.SetParent(null);
}
}
 
Last edited:
What view type are you using? In the demo scene the view type switches to the follow view type for third person.
 
Thanks Im on adventure view, I have just compared to the demo scene. It looks mostly identical in terms of settings but I didn't have camera combat view.

I have tried different set ups including combat view, and adding freeze & anchoring the player camera and entering character as follow (in camera settings) but no real solution. I just cant get my head around the causes a sudden spin on player being dead. It's not a new issue though, it began when I started the project too but thought it would be an easy fix and didn't really bother me at the time.

*It's like the players movement is passed on to the camera when player dies i.e if rotating right on isdead then the player will likely spin right if its is neutral then wont spin.

*So far I have used disabled the input which I'm not sure really resolves as it still often happens

Should I be switching cameras / view type on Isdead?
 
Last edited:
I don't recommend using adventure or combat for when the character dies. You should instead switch to a follow view type using the state system. When the die or ragdoll ability activates you can then enable a follow preset on the camera controller.
 
This page has a good overview of the state system, with the API:


In this case you don't need any code though. When the Die or Ragdoll ability activates then it should activate a new state on the Camera Controller.
 
Thanks. For some reason I personally struggled with the State system & how to enable a follow preset on the camera controller, does it get dropped into a field in the die ability?
Code:
StateManager.SetState(m_Character, "LookAtPresetTest", true);// could'nt get this to work this way
I watched over a few times too, I'll keep trying. Be good to see an example video tutorial directly associated with the state camera switch & death scenario. I did watch another Opsive state system video with camera set up, but state configuration is missing in my version, in the video though describes a state configuration option after right click. I cant get the logic for when the switch happens this way. Yes good point on view change when player dies. The 'Look at view' / follow view as mentioned in the documents for death scenario, seems ideal as the right stick wont work in this view, so movement wont be active on death. If I could just get this to switch would be ideal. However by disabling input with the event handler I'm attempting a similar effect, in terms of preventing movement. The idea of easily swapping camera views seems very useful is there an easier solution to help me grasp this?

Code:
 EventHandler.ExecuteEvent(Player, "OnEnableGameplayInput", false);//*
*This EventHandler method works but not been 100% fix as the input stops but wont always work and will spin at times, I have placed as top of order of events to see if there is a further improvement.

Ideally though figuring out swapping 2 camera views set up for each scenario (the third person adventure view (when active), third person look at view (when player dies) just be good to be able to easily switch between the 2? **Update figured code from camera API as below

Alternatively I did try below with the API 'view types' in Opsive documents too as this seemed like it could work but had no effect. Should this method work?


Code:
        if (playerCurrentHealth < 0 && !isDead) // I cant see why this is not switching here seems a logical method
 
        {
            {
                var camera = CameraUtility.FindCamera(null);// new
                if (camera == null)
                {
                    return;
                }

                var followViewType = camera.GetComponent<CameraController>().GetViewType<Opsive.UltimateCharacterController.ThirdPersonController.Camera.ViewTypes.LookAt>();
                if (followViewType != null)
                {
                    Debug.Log("Found the followview/lookat view type.");
                }
 
Last edited:
On the Ragdoll ability you can set a state:

1688535882066.png

This state is then used by the Camera Controller to set a new ViewType:

1688535920048.png
 
Thanks. I carefully watched both videos. I set it similar to as described above I don't have ragdoll method though, selected death in state drop down. Created a pre-set on third person lookat view & pressing persist & activate. l get the logic, but doesn't work for me some reason. I checked through states testing death, aim, run to try force a camera switch to occur, none caused the switch either. It's as if the saved pre-sets are not being registered, even when repeating steps in the video. Basically new pre-sets are not saving. I tried saving presets in other locations in asset folder presets or directly in my project level file either way not worked. I tried using the demo pre-set thought that may be a work around.

I tried clicking on the plus sign creating new LookAt preset nothing happens. Experimented by adding the demo Enable Look at at view pre-set but get an error doesn't use same object type. Other demo pre-sets do work like the ones in my project when I first created a character. Some of the demo pre-sets work but not all like Enable Look and creating a new one wont register. Creating a new state needs more information and clarification as its a very useful idea. Do I need to create a new camera? Are pres-sets created & saved like prerequisites in Opsive main manager?

2.5 mins into state configuration and profiles tutorial says 'if you want to create your new profile you can by right clicking,
'add state configuration create new file' I couldn't locate this in my drop down menu but in the video theres an option, I clicked the same area as the video etc. I don't have demo state configuration available either camera settings unlike shown in the video.
Be good to have more simplified and documents & tutorials creating state pre-sets. I'm just finding it hard to learn. Ideally talking through the steps involved from start to finish as if anyone could replicate those steps and get to where they need to be. I would say the state system and character swapping are 2 strong areas but needing clarifying which would really improve UCC.

I get there must be lots to explain on UCC limited time for video tutorials. Everything about UCC is brilliant and I am pleased with the way it works and progress made and the high quality etc.

Finally got the code to work. looks like the API I used before did'nt work. The Camera API with shared Utility (under Camera in Opsive documents is working fine but the one under View types wasn't working in this situation).

Below used the Opsive API to switch to LookAt as the player dies (1st script below) then back to adventure (2nd script below). This works great giving camera contol options after after the player dies. Just be good to have more state presets information.

Code:
 // This worked for me using these 2
using Opsive.Shared.Camera;// *Must be implemented to script
using Opsive.Shared.Utility;// *This one works Must be implemented to script
using Opsive.UltimateCharacterController.Camera;*Must be implemented to script

public class MyObject : MonoBehaviour// 1st script LookAt veiw for death of player so players movement is no carried. 2nd return to original view.
{
    [Tooltip("The character that should be assigned to the camera.")]
    protected GameObject m_Character;

    /// <summary>
    /// Sets a third person perspective on the Camera Controller.
    /// </summary>
    private void Start()
    {
        var camera = CameraUtility.FindCamera(null);
        if (camera == null) {
            return;
        }

        var cameraController = camera.GetComponent<CameraController>();
        cameraController.Character = m_Character;
        cameraController.SetPerspective(false); // false indicates the third person perspective.

        // Switch to the third person Combat View Type.
        var viewTypeName = "Opsive.UltimateCharacterController.ThirdPersonController.Camera.ViewTypes.LookAt";
        cameraController.SetViewType(TypeUtility.GetType(viewTypeName), false);
    }
}
Code:
using Opsive.Shared.Camera;// For returning to original view
using Opsive.Shared.Utility;//
using Opsive.UltimateCharacterController.Camera;

public class MyObject : MonoBehaviour
{
    [Tooltip("The character that should be assigned to the camera.")]
    protected GameObject m_Character;

    /// <summary>
    /// Sets a third person perspective on the Camera Controller.
    /// </summary>
    private void Start()
    {
        var camera = CameraUtility.FindCamera(null);
        if (camera == null) {
          // return;  no return needed on 2nd here if combining both scripts in your code
        }

        var cameraController = camera.GetComponent<CameraController>();
        cameraController.Character = m_Character;
        cameraController.SetPerspective(false); // false indicates the third person perspective.

        // Switch to the third person Combat View Type.
        var viewTypeName = "Opsive.UltimateCharacterController.ThirdPersonController.Camera.ViewTypes.Adventure";
        cameraController.SetViewType(TypeUtility.GetType(viewTypeName), false);
 
Last edited:
Top