- Home /
Create an Enemy Animation
Hi!
I´m new in Unity. I'm trying ti create an Enemy which can throw a ball to an object. I created tow animations for that. One is the "waiting" animation that is a loop and repeats over and over again. Between this loop there is a "fire" animation which the Enemy Throw a ball to this object. The enemy have to spawn the ball in the hand of it every time it was thrown...
Probably because I'm new it is hard...
Can someone help me?
Thanks!!
PD: Sorry for my english...
Answer by Nik-60 · Jun 23, 2015 at 12:48 PM
First of all declare two variables, A Gameobject and a Rigidbody
private GameObject Projectile_Spawn;
public Rigidbody Projectile;
Under the start function find the object hand of your enemy
Projectile_Spawn = GameObject.Find ("Hand");
And finally within the enemy condition start a coroutine
StartCoroutine (ShootProjectiles);
And finally the Coroutine
IEnumerator ShootProjectiles() {
Anim.Play ("Your_Shoot_Animation");
Rigidbody shot = Instantiate (Projectile, Projectile_Spawn.transform.position, Projectile_Spawn.transform.rotation) as Rigidbody;
shot.velocity = transform.TransformDirection (Vector3.forward * 5);
}
Also check the Unity Scripting API
There are tons of example code's in both C# and Java. Happy learning...
Regards,Nik-60
Answer by Cherno · Jun 22, 2015 at 04:24 PM
Hello,
the first stop for finding out how things work should always be the Unity Scripting API and the Unity User Manual. To make your enemy throw the object, you need to know about three things: 1. The animations (which you already have), and how to play them (this differs depending on wether you use the old Animation component, or the newer Animator component). 2. A prefab of an object that will be spawned in the hand, and the Instantiate function to make a copy of this prefab in the game world 3. A script on the object to make it move towards the target position. Vector3.Lerp or Transform.Translate would be simple solutions, but for realistic behavior you might want to give the ball or whatever a RigidBody and use the AddForce function when it is thrown.
Your answer
Follow this Question
Related Questions
Joints & Throws 1 Answer
Can I make animations snap to a frame? 1 Answer
Enemy offset, gallaga style game (space invaders) 0 Answers
How to make an enemy pace? 1 Answer