Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by link124 · Aug 28, 2016 at 06:50 PM · 2d gameshootingprojectile2d animationspace shooter

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;
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image link124 · Aug 29, 2016 at 01:48 PM 0
Share

If anyone know a video that can help please post. Thank you

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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)));

}

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make projectiles shoot diagonally? 1 Answer

How do I stop a 2D projectile when it reaches a point 3 Answers

2D Animations Not Working After Building Game 1 Answer

Projectile Shooting Problem 1 Answer

Jump animation isn't working while running 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges