Search Unity

Simple: Play Animation from Child Object [my script is included]

Discussion in 'Scripting' started by Rockinator, Aug 22, 2014.

  1. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    using UnityEngine;
    using System.Collections;

    public class PressScriptTemp : MonoBehaviour {

    public GUIText TextOutput; //for the text to pop out on screen
    public AudioClip SoundToPlay; //for you to drop a sound in

    void OnTriggerEnter (Collider other)
    {
    transform.parent.animation.Play("doorLift"); //animation for the door
    animation.Play("buttonPress"); //animation for the pressure plate
    AudioSource.PlayClipAtPoint (SoundToPlay, this.transform.position); //sound
    TextOutput.text = ""; //text if you want to notify the player of what is happening
    }

    void OnTriggerExit (Collider other)
    {
    transform.parent.animation.Play("doorDrop"); //animation for the door
    animation.Play("buttonLift"); //animation for the pressure plate
    AudioSource.PlayClipAtPoint (SoundToPlay, this.transform.position); //sound
    TextOutput.text = ""; //text if you want to notify the player of what is happening
    }
    }


    So this here is the set I have for the pressure plates in my level, what I need is for an animation to be called from the child. So, that this line of code:

    transform.parent.animation.Play("doorDrop"); //animation for the door
    animation.Play("buttonLift"); //animation for the pressure plate

    I am calling the parent animation as well as the animation on the object containing this script. Simple enough, though the problem is that since the parented object is moving so is the child in which this script is on. My solution? I want to make an empty gameObject with the trigger and place both objects(the plate and door) as children to the empty gameObject. So what do I need help on? I would like to know how to script play animation from child object. I know this is simple but I am still fairly new at this, the only thing I know is "GetComponentInChildren" this makes sense to me but how do I use this to play my animations?
     
  2. Karwler

    Karwler

    Joined:
    Aug 25, 2014
    Posts:
    18
    I am not sure if it is what you ask for, but I have a quite simple idea:
    First of all create two variable like this two:
    Code (CSharp):
    1. public Transform Door;
    2. public Transform Button;
    Now try to call the animations like this:
    Code (CSharp):
    1. Door.animation.Play("doorDrop");
    2. Button.animation.Play("buttonLift");
    Hit [F7] go back to the editor select the object to which you have attached the script and drag and drop the two Objects to the two new two variable windows.
    Hope it was helpfull ;)
     
    Rockinator likes this.
  3. Rockinator

    Rockinator

    Joined:
    Jul 25, 2014
    Posts:
    22
    I actually figured it out, it would just be easier to get a empty game object and just parent it to the plate and door. Such an obvious fix! Then, you only need to call the animation that is in the empty gameobject parent! Lol, like I said, easy fix but my brain wasn't working at that time. Thank you for the reply though!