Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access Animator Controller from other objects?

Discussion in 'Scripting' started by RuanJacobz, Dec 10, 2014.

  1. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Hey guys, I made a simple camera shake state and animation in Mecanim. I have a player character and I want it to trigger this camera shake when he actually shoots. My shoot script is here below:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerShoot : MonoBehaviour
    5. {
    6.     public GameObject bullet;
    7.     public string shotButton = "Fire1";
    8.     public float shotDelay = 0.2f;
    9.     private bool readyToShoot = true;
    10.  
    11.     // Update is called once per frame
    12.     void Update ()
    13.     {
    14.         {
    15.             if(Input.GetButton(shotButton) && readyToShoot)
    16.             {
    17.                 if(audio != null)
    18.                 {
    19.                     if(!audio.isPlaying)
    20.                     {
    21.                         audio.Play();
    22.                     }
    23.                 }
    24.                 Instantiate(bullet,transform.position,transform.rotation);
    25.                 readyToShoot = false;
    26.                 Invoke("ResetReadyToShoot", shotDelay);
    27.             }
    28.         }
    29.     }
    30.    
    31.     void ResetReadyToShoot()
    32.     {
    33.         readyToShoot = true;
    34.     }
    35. }
    So every 0.2 seconds my character shoots. I also want it to play the camera shake animation every 0.2 seconds. My issue is I have no idea how to do this. I added a bool parameter in the cameraController called "Shake" and set it as the transition condition for the camera to go from idle to shaking.

    How do I actually set that bool to true all the way from my player's shooting script?
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Just create a var for the scipt you want to access in you player script. Preferably, set it in the editor.

    Code (csharp):
    1.  
    2. public someScript someScript_data;
    3.  
     
    RuanJacobz likes this.
  3. RuanJacobz

    RuanJacobz

    Joined:
    Jan 24, 2014
    Posts:
    59
    Thank you for reply mate.

    What I ended up doing is a bit hacky, but works brilliantly so far. I added a big collider to the camera and put it on a layer called "CameraCollider". I put a script on the camera that sais whenever something enters this trigger collider "shake" must be set to true. And it must be set to false after .3 seconds. I then adjusted the collision matrix so that player bullets and explosions can collide with camera, and nothing else. So far this is working flawlessly.