- Home /
 
How to shoot a bullet and animate at the same time??? Its a 2D shooter c#
Hi everyone i need help i want to make my character change animation to attack. i got the player to create a projectile but now i want when i press Space bar the player change to an attacking animation and the bullet created.
 public GameObject BulletShotRight;
 public GameObject BulletPos1;
 public float speed;
 // Use this for initialization
 void Start () 
 {
 }
 void Update () {
     if (Input.GetKeyDown("space"))
     {
 
               //This line create the bullet. Is there a way to add the animation here? GameObject bullet01 = (GameObject)Instantiate (BulletShotRight); bullet01.transform.position = BulletPos1.transform.position;
     }
     float x = Input.GetAxisRaw("Horizontal");
     float y = Input.GetAxisRaw("Vertical");
     Vector2 direction = new Vector2 (x, y).normalized;
     Move (direction);
 }
 void Move(Vector2 direction)
 {
     Vector2 min = Camera.main.ViewportToWorldPoint (new Vector2 (0, 0));
     Vector2 max = Camera.main.ViewportToWorldPoint (new Vector2 (1, 1));
     max.x = max.x - 0.225f;
     max.x = max.x + 0.225f;
     max.y = max.y - 0.285f;
     max.y = max.y + 0.285f;
 
     Vector2 pos = transform.position;
 
     pos += direction * speed * Time.deltaTime;
 
     pos.x = Mathf.Clamp (pos.x, min.x, max.x);
     pos.y = Mathf.Clamp (pos.y, min.y, max.y);
 
     transform.position = pos;
 }
 
               }
If anyone know a video that can help please post. Thank you
Answer by Dream_in_code · Aug 29, 2016 at 04:19 AM
You can do it in a easy way.Create a projectile script.
 // Use this for initialization
 public float projectilespeed;
 Rigidbody2D myrigidbody;
 
 // Use this for initialization
 void Awake()
 {
     myrigidbody = GetComponent<Rigidbody2D>();
     myrigidbody.AddForce(new Vector2(1, 0) * rojectilespeed, ForceMode2D.Impulse);
 }
 // Update is called once per frame
 void Update()
 {
 }
 public void removeForce()
 {
     myrigidbody.velocity = new Vector2(0, 0);
 }
 void OnTriggerEnter2D(Collider2D other)
 {
     if (other.gameObject.layer == LayerMask.NameToLayer("What do you wan to shoot?"))
     {
         removeForce();
       
         Destroy(gameObject);
     }
 }
 
               Now inside player script
//bullet variables
 public Transform ProjectileLocation; //from where the projectile will get released
 public GameObject bullet; //insert you bullet inside here. Attach the projectile script also.
 
               void FixedUpdate()
{
attack();
}
public void attack()
{
if (Input.GetKeyDown(KeyCode.Space))
{
//play the animation here. ..If you dont know about attack animations check this>> https://www.youtube.com/watch?v=-gC1u7qHMY4
Instantiate(bullet, Projectilelocation.position, Quaternion.Euler(new Vector3(0, 0, 0)));
}
}
Answer by link124 · Aug 29, 2016 at 05:25 PM
@Dream_in_code Thanks for your help but i still cant get the animation to play. Unity keep telling me that the Field ( in my case it the this : PlayerControl.myAnimator) is never assigned to, and will always have its default value null.
Your answer