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 OzzyRawrz · Apr 08, 2012 at 07:21 AM · animationjavascriptcolliderbooleans

Animation Script not working.

i want my character to use the punch animation on colliding with the player but its not working this is the script

 var Myself : Transform;
 
 var AttackorNot : int;
 
 function Update () {
 
  
 
           if(AttackorNot == 1){
 
              Myself.animation.CrossFade("punch");
 
             }
 
     
 
     }
 
  
 
 function OnTriggerEnter(other:Collider){
 
    
 
    
 
        if(other.gameObject.CompareTag("Player"))
 
    {
 
  
 
       AttackorNot = 1;
 
  
 
    }
 
 }

Myself is the main object which have this trigger so i set this boolean function that if it will collide with the player the boolean will be 1 and in the update function i used this boolean function to run the animation on the main Object.

its not working. whats wrong in it?

Comment
Add comment · Show 5
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 GuyTidhar · Apr 08, 2012 at 07:32 AM 0
Share

Is the collider marked as "IsTrigger"? Do you have a rigidbody on one of the components? A characterController?

avatar image OzzyRawrz · Apr 08, 2012 at 07:41 AM 0
Share

yes it is marked as trigger and it have the rigidbody component

avatar image GuyTidhar · Apr 08, 2012 at 08:21 AM 0
Share

I have 2 remarks on $$anonymous$$elptomaniac's note: 1. Use CompareTag ins$$anonymous$$d of the == operator. It is faster and does not allocate memory like regular string comparisons. Could be noticeable when doing allot of such comparisons, so you original take is good. Better to get use to it anyways. 2. It is also a good practice to allocate a reference to myTransform, as it is also a bit faster then doing repeating calls to gameObject.transform (or transform which is the same). What you could do, to make it a bit more simple, is myself = transform in the Awake() or Start() functions.

avatar image GuyTidhar · Apr 08, 2012 at 08:26 AM 0
Share

Have you tried to automatically play the punch animation when you run the game? So to make sure you don't have a more basic problem?

avatar image Kleptomaniac · Apr 08, 2012 at 11:03 AM 0
Share

Thanks, @guyt. I didn't actually realise what CompareTag did until I just looked up the docs now. I'll make sure to use this from now on. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Kleptomaniac · Apr 08, 2012 at 07:32 AM

OK, this script needs a bit of cleaning up. First of all, don't use CompareTag, just look for other.gameObject.tag and determine if it's "Player". Second of all, you will instead want the object with the "Player" tag to have isTrigger checked. Third of all, you don't need to specify the gameObject your script is attached to, because the compiler defaults to it if a specific object is not given. Therefore, your "Myself" variable is redundant. Also, you should try using camelCase for variable names so that they show correctly in the inspector. So like this:

 var myself : Transform;
 var attackOrNot : int;
     
     function Update () {
         if(attackOrNot == 1){
             myself.animation.CrossFade("punch");
         }
     }
     
     function OnTriggerEnter(other:Collider){
         if(other.gameObject.tag == "Player") {
             attackOrNot = 1;
         }
     }

I believe that should work. By the way, attackOrNot isn't a boolean, it's an int. :P

Hope that helps, Klep

Comment
Add comment · Show 9 · 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 OzzyRawrz · Apr 08, 2012 at 07:38 AM 0
Share

i know its an int o-o but i was using it as a boolean function like 0 or 1 and I dont want the main object to be triggered x-o i just want to put this script on a trigger which is a child of the main object and its an empty Object im just using it as a box collider trigger

avatar image Kleptomaniac · Apr 08, 2012 at 07:48 AM 0
Share

O$$anonymous$$, so "$$anonymous$$yself" was actually an empty child of the gameObject with script attached? And you had it as the trigger? Well, that still wouldn't work, because "other" is the collider with the trigger. The gameObject with the "Player" tag would have to have the trigger.

I'll edit "$$anonymous$$yself" back in ... I didn't realise it was a child.

avatar image OzzyRawrz · Apr 08, 2012 at 07:53 AM 0
Share

nothing changed x-o the same thing i did... its not working either ._.

avatar image Kleptomaniac · Apr 08, 2012 at 07:59 AM 0
Share

So

1) Is there actually an animation with the name "punch" (case-sensitive) on your $$anonymous$$yself gameObject?

2) Try animation.Play("punch") ins$$anonymous$$d of Crossfade("punch") and see if that works.

3) Did you set the trigger up on your "Player" object ins$$anonymous$$d and see if that did anything different?

4) Try chucking a Debug.Log inside your OnTriggerEnter and see if it actually gets past there.

avatar image OzzyRawrz · Apr 08, 2012 at 07:59 AM 0
Share

well while playing the game i saw that the AttackorNot int Number was changed on Collider with player from 0 to 1. but the animation is just not running

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

Punch Animation Doesn't Work And Others Do... 1 Answer

How to display GUI in sequence in trigger 3 Answers

Animation play when dead 2 Answers

Door open/load level java script troubles 1 Answer

Java Script: Clock script help. 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