- Home /
A simple basic script: play animation on collision
I am a big noob in scripting, but i am working on a game so i can learn it.
I am making a scary game, i placed a cube and made it invisible and that you could walk through it. I want that if the firstpersoncontroller is walking through it, a animation has to start of a ghost that is flying towards you. Is there a simple script that let's you do that. Let's say that the animation is called: Ghost1 and the cube is called: trigger1
I searched for like a hour but i only found scripts that were not compleet and because i am a noob in scripting i couldn't get it to work.
Also, can i add a sound to the animation or do i need another script for that?
Answer by ScroodgeM · Jul 20, 2012 at 04:26 AM
- make a collider on character
make c collider on cube and mark it as trigger
make a simple script and attach it to cube
make a method that fires when something enters a trigger (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html)
in thins method you got other collider in inputs, get a character from it using 'collider.gameObject'
apply an animation switch to character (http://docs.unity3d.com/Documentation/ScriptReference/Animation.CrossFade.html)
write here if you stuck anywhere. be ready to try it yourself and we will help to finish the task 8)
what i have now is:
function OnTriggerEnter (other : Collider) {
animation.Play("Ghost1"); }
but how can i let the ghost do the animation because now the cube is doing the animation.
also how can i activate the sound, something like?
audio.Play("Name");
calling animation of other object:
this line
animation.Play("Ghost1");plays the animation of object that script attached to. you should declare in this script an instance of other object
GameObject ghostObject;and call animation at this object
ghostObject.animation.Play("Ghost1");don't forget to attach ghost's gameobject to script
making audio:
add an 'audioSource' component to ghost, assign audioClip (audio file) to this source, and call audio.Play() when you need sound.
http://docs.unity3d.com/Documentation/Components/class-AudioSource.html
thx for the reply
What i have now is:
function OnTriggerEnter (other : Collider) { GameObject ghostObject; ghostObject.animation.Play("Ghost1"); ghostObject.audio.Play("Ghostsound"); }
but how do i attach the ghost to the script? because the script don't know what ghostObject is. do i have to do:
GameObject ghostObject("ghost");
and the audio is giving me a error:
Assets/Standard Assets/Scripts/Activate animation.js(4,15): BCE0023: No appropriate version of 'UnityEngine.AudioSource.Play' for the argument list '(String)' was found.
http://docs.unity3d.com/Documentation/Images/manual/ScriptingTutorial.pdf
chapter 5
it's a scripting basics...
using UnityEngine; using System.Collections; public class AnimationOnTriggerEnter : $$anonymous$$onoBehaviour { public GameObject AnimatedObject; void OnTriggerEnter(Collider other) { AnimatedObject.animation.CrossFade("Fire"); AnimatedObject.audio.Play(); } }
don't forget to attach animated GO to this script
one of objects with collider (trigger or collider that enters trigger) should have a rigidbody
attached object should have animation and audiosource component