- Home /
Fire Animation Help
Ok guys, I have a script attached to an empty called "SpawnPoint". The SpawnPoint is attached to my gun. I want it to play an animation when I press the required key. here is the script:
var prefabBullet:Transform; var shootforce:float;
function Update () { if(Input.GetButtonDown("Fire2")) { var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootforce); }
if(Input.GetButtonDown("Fire2"))
{
animation.Play("Aim-FIRE!");
}
}
The firing itself works well enough, but the animation refuses to play. This is the error I get:
MissingComponentException: There is no 'Animation' attached to the "SpawnPoint" game object, but a script is trying to access it. You probably need to add a Animation to the game object "SpawnPoint". Or your script needs to check if the component is attached before using it. UnityEngine.Animation.Play (System.String animation) fire.Update () (at Assets/fire.js:15)
Is there a way to get it to access the script attached to my player?
Answer by MonkeyAssassin8 · Mar 13, 2011 at 02:09 AM
private var player : GameObject;
function Start(){ player = GameObject.FindWithTag("Player"); }
function Update(){ if(Input.GetButtonDown("Fire2")){ player.gameObject.animation.Play("Aim-FIRE!"); } }
Answer by by0log1c · Mar 13, 2011 at 06:23 AM
I think you just need to add an 'Animation' Component from the Component Menu onto the Spawnpoint GameObject. No animation component = no animation playing. ;)
Answer by Sajid 1 · Mar 13, 2011 at 05:18 PM
This is what I got for the script you gave me:
var prefabBullet:Transform; var shootforce:float;
function Update () { if(Input.GetButtonDown("Fire2")) { var instanceBullet = Instantiate(prefabBullet, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootforce); } }
private var player : GameObject;
function Start(){ player = GameObject.FindWithTag("Player"); }
function Update(){ if(Input.GetButtonDown("Fire2")){ player.gameObject.animation.Play("Aim-FIRE!"); } }
This is the error I got: Assets/fire.js(19,10): BCE0089: Type 'fire' already has a definition for 'Update()'.
Im sorry, but i'm a nooby at scripting.
it's because you have 2 functions of the same name. put player.gameObject.animation.Play("Aim-FIRE!"); into your update function and put player = GameObject.FindWithTag("Player"); into your start function, you then delete the functions that are unused. hope you can understand this :P
Your answer
Follow this Question
Related Questions
Walking Animation 1 Answer
How do I play my animations w. Mechanim? 1 Answer
Play animation in child model 0 Answers
On collision play animation 1 Answer
Retrieving the name of the animation currently playing 9 Answers