- Home /
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?
Is the collider marked as "IsTrigger"? Do you have a rigidbody on one of the components? A characterController?
yes it is marked as trigger and it have the rigidbody component
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.
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?
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. :)
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
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
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.
nothing changed x-o the same thing i did... its not working either ._.
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.
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
Your answer
Follow this Question
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