- Home /
Animating main player when click on object
Hii,,
I am new to unity as well as scripting plz bare me..
i am trying to play an animation of my main player when i click on the other object...
for instance i am trying to do script(Java only) to pick up an object from ground..
i have a small cube on my plane now i want to play my pick up animation (Which is on my player)& collect that cube when i click on that cube...
plz plz plz help me out..
There is another problem when i used that animation on my player using following clip
if ( Input.GetMouseButton(2) ) { animation.Play("Pick up"); } } then it play animation only when i keep press the said button
Actuly i want to play full animation when i click once.
(Sorry for my bad English as it was not my language)
if you put inspector to Debug mode click the animation. is there a number 2 or 1 in the animation type box?
sorry, reread you're script where it says if(Input.Get$$anonymous$$ouseButton)
it should be if(Input.GetButtonDown("Fire2"))
Thank you for reply
As i see animation show 1 number in debug mode
and other scrip gives me a same effect....
hmmm, do u have Skype maybe if i could see what you mean and look at your scripts it would help me help you.
Answer by $$anonymous$$ · Mar 23, 2014 at 03:35 PM
this is what i would use:
Item that you want to be picked up as a script put a collider on it and set it as a trigger.
script on the item should say:
var Player : Transform;
OnTriggerEnter()
{
Player.SendMessage("PickUp");
}
script on player
function PickUp()
{
if(Input.GetButtonDown"Fire2")
{
animation.CrossFade("animationname");
}
}
Answer by ivan2532 · Mar 23, 2014 at 02:10 PM
Hello Foolish_Smart_,
There are bunch of ways to do this, I will show you one way:
Create a JavaScript and write down this code:
var playerObject : GameObject; // attach player game object in inspector
var pickUpAnim : AnimationClip; // attach animation file in inspector
function OnMouseDown()
{
//here insert your code for pick up
playerObject.animation.CrossFade(pickUpAnim.name);
}
Attach it to the item.
Good luck, Ivan
That won't work as whenever you press the mouse button the animation will play and that is why i suggested the on trigger method as you will have to be next to the object
Thanks both of you i tried both of your scripts as far as considering yolo22 script it is better view but wont work as trigger function is work properly but it not animate a player.. I think the function is not calling a pick up function from player as player enter in the trigger..What should i do..
As far as considering $$anonymous$$script it works but when ever i click on the object it got animated irrespective of position of the object ..what should i do to fixed it ..
I would like to learn both the ways as i am in learning phase ..
once again thanks for your valuable inputs..
The Pick-up Function is like when you send a message it does nothing unless you define it.
one thing could be the collider is too small as it goes from the centre of the player not any part of it so maybe try increasing the size?
if you have g mail, we could use Google plus maybe so i could see what is happening. or screen record with Debug.Logs scattered through out the script so i can see where the problem is.
if so here is my Email: $$anonymous$$
I email you totally appreciate your help thanks a lot..:)