Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hansolo776 · Jul 19, 2012 at 07:37 PM · animationcollisiontrigger

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Jul 20, 2012 at 04:26 AM

  1. make a collider on character
    1. make c collider on cube and mark it as trigger

    2. make a simple script and attach it to cube

    3. make a method that fires when something enters a trigger (http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html)

    4. in thins method you got other collider in inputs, get a character from it using 'collider.gameObject'

    5. 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)

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hansolo776 · Jul 20, 2012 at 03:44 PM 0
Share

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");

avatar image ScroodgeM · Jul 20, 2012 at 04:24 PM 0
Share

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

avatar image Hansolo776 · Jul 20, 2012 at 06:02 PM 0
Share

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.

avatar image ScroodgeM · Jul 20, 2012 at 06:20 PM 0
Share

http://docs.unity3d.com/Documentation/Images/manual/ScriptingTutorial.pdf

chapter 5

it's a scripting basics...

avatar image ScroodgeM · Jul 20, 2012 at 09:35 PM 1
Share

using UnityEngine;
using System.Collections;
public class AnimationOnTriggerEnter : $$anonymous$$onoBehaviour
{
    public GameObject AnimatedObject;
    void OnTriggerEnter(Collider other)
    {
        AnimatedObject.animation.CrossFade("Fire");
        AnimatedObject.audio.Play();
    }
}
  1. don't forget to attach animated GO to this script

  2. one of objects with collider (trigger or collider that enters trigger) should have a rigidbody

  3. attached object should have animation and audiosource component

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to handle animation of multiple Coins? 1 Answer

How do I make animation transitions a one-way street? 1 Answer

Only Collide When Certain Animation is Not Playing 0 Answers

Play Animation OnTriggerEnter 2 Answers

Presure plate that triggers an animation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges