CutScene with some turn off and turn on components.

Hello everybody I create two scripts and they should start cutscene and end it. Also with some turn off component so player cant move while cutscene, but I am not so experienced with Scripting, and I made a few mistakes because this script doesn’t work at all.

Here is the first Script what do Raycasting

 RaycastHit cellPhone;
        Ray phoneRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
        if (Physics.Raycast(phoneRay, out cellPhone, PhoneRayDistance))
        {
            if (hit.transform.gameObject.tag == "Phone")
            {
                if (Input.GetButtonDown("actButton"))
                {
                    GetComponent<phoneAnimationScript>().CutSceneStart();
                    Debug.Log("RayHitPhone");
                }
                Debug.Log("RayHitPhone");
            }
        }
        }

And a second one what should turn off components and start an animation

public class phoneAnimationScript : MonoBehaviour {
    private Animator cellPhoneAnimatorCtrl;
    public Animation cellPhoneAnimation;
    private AnimatorStateInfo BaseLayer;
    static int answerPhoneAct = Animator.StringToHash("Base Layer.phoneAnswerAnim");
    private AnimatorStateInfo currentBaseState;
    public CharacterMotor MovementScript;
    public MouseLook CameraMovementScript;
  
    void Start () {
        cellPhoneAnimatorCtrl = GetComponent<Animator>();
        cellPhoneAnimation = GetComponent<Animation>();
        MovementScript = GetComponent<CharacterMotor>();
        CameraMovementScript = GetComponent<MouseLook>();
    }
    public IEnumerator CutSceneStart()
    {
      
            PhoneAnimationStart();
            CharacterAnimationStart();
            yield return new WaitForSeconds(5);
            TurnOnEverything();
      
      
    }
    public void PhoneAnimationStart()
    {
        cellPhoneAnimation.Play("take a phone");
    }
    public void CharacterAnimationStart()
    {
      
        MovementScript.enabled = false;
        CameraMovementScript.enabled = false;
        cellPhoneAnimatorCtrl.SetBool("phoneAnswer", true);
          
    }
    public void TurnOnEverything()
    {
        MovementScript.enabled = true;
        CameraMovementScript.enabled = true;
    }
}

Can someone help please.

This is probably your problem GetComponent<phoneAnimationScript>().CutSceneStart();

This doesn’t start the function as a coroutine. What you want to do is

phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
StartCoroutine(pas.CutSceneStart());

Just tried your code, animation didn’t starts.

Alsow DebugWindow say this part of code has a Null reference

StartCoroutine(pas.CutSceneStart());

For GetComponent to work, the phoneAnimationScript needs to be on the same gameobject. If it’s not, then it will return null.

If you don’t want them on the same gameobject, you can create a reference to it that you assign in the inspector.

public phoneAnimationScript myPhoneAnimationScript;
...
StartCoroutine(myPhoneAnimationScript.CutSceneStart());

Yeah it give some progress now Animation from phone starts, but character animation is not.

I tried to add created a reference for character too, but it gives me an error

    public phoneAnimationScript myphoneAnimationScript;
    public phoneAnimationScript myCharacterAnimationScript;


if (Input.GetButtonDown("actButton"))
                {
                    phoneAnimationScript pas = GetComponent<phoneAnimationScript>();
                    StartCoroutine(myphoneAnimationScript.CutSceneStart());
                    StartCoroutine(myCharacterAnimationScript.CutSceneStart());
                }

(MissingComponentException: There is no ‘Animation’ attached to the “Player” game object, but a script is trying to access it.

You probably need to add a Animation to the game object “Player”. Or your script needs to check if the component is attached before using it.)

My character is controlled by a AnimationController, Should I add separate Animation component for this action?

Animation is the legacy animation system, Animator is the new mecanim system. If you’re using an AnimationController, you should be using Animator and not Animation.

Yeah sorry didnt know Animation and Animator dont work good with each other. Thank you so much for help, no kisses but I am sure you understand =)

Here is fully workable example in case some one will be looking for the same as I. Thanks to steego

public class phoneAnimationScript : MonoBehaviour {
    private Animator cellPhoneAnimatorCtrl;
    private Animator cellPhonePickUP;
    static int pickPhone = Animator.StringToHash("Base Layer.take a phone");
    static int answerPhoneAct = Animator.StringToHash("Base Layer.phoneAnswerAnim");
    private AnimatorStateInfo currentBaseState;
    public CharacterMotor MovementScript;
    public MouseLook CameraMovementScript;
   
    void Start () {
        cellPhoneAnimatorCtrl = GetComponent<Animator>();
        cellPhonePickUP = GetComponent<Animator>();
        MovementScript = GetComponent<CharacterMotor>();
        CameraMovementScript = GetComponent<MouseLook>();
    }
    public IEnumerator CutSceneStart()
    {
       
            PhoneAnimationStart();
            CharacterAnimationStart();
            yield return new WaitForSeconds(5);
            TurnOnEverything();
            Debug.Log("Corrutine_Start");
       
       
    }
    public void PhoneAnimationStart()
    {
        cellPhonePickUP.SetBool("pickPhone", true);
    }
    public void CharacterAnimationStart()
    {
       
        MovementScript.enabled = false;
        CameraMovementScript.enabled = false;
        cellPhoneAnimatorCtrl.SetBool ("phoneAnswer", true);
           
    }
    public void TurnOnEverything()
    {
        MovementScript.enabled = true;
        CameraMovementScript.enabled = true;
    }
}
1 Like

Yes, that’s quite alright :slight_smile: