Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jamal12 · Jun 13, 2015 at 09:56 PM · 2dangleprojectileangles

How do I fire a 2D projectile based on it's current angle/location.

I have a project for class in which I have to make a turret constantly rotate and occaisonally shoot a bullet from the barrel of the turret.

each rotation does the following below.

         turretObject.transform.eulerAngles = new Vector3(turretObject.transform.eulerAngles.x,turretObject.transform.eulerAngles.y,newZ);
         GameObject tempBullet = Instantiate (projectile, projectileLoc, Quaternion.identity) as GameObject;
         tempBullet.transform.eulerAngles = projectileAngle;


in my projectile script(the script attatched to my bullet) every 50Ms I have a corrutine doing this.

     IEnumerator projectileTick(float ms)
     {
         yield return new WaitForSeconds (ms / 1000);
         lifeTime -= 50f;
         if (lifeTime == 0f && currentProjectile != null) 
             Destroy (currentProjectile);
 
 
         currentProjectile.rigidbody2D.velocity = -transform.right * 5;
         print ("here");
         if (currentProjectile != null) 
             StartCoroutine (projectileTick (50));
         
     }

the result is this

alt text

I want the bullet to fire based on the angle its facing however I cannot seem to get that to work properly any help would be appreciated.

failure.png (1.8 kB)
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 Guppie1337 · Jun 14, 2015 at 12:25 PM 0
Share

I've recently done something similar, I think. $$anonymous$$aybe you can try this: Give the end of the barrel an empty child that is used solely for the purpose of it's transform. Whenever you instantiate a bullet, you would make it spawn at that child's position with that child's rotation. Your bullet's instruction could be literally as simple as transform.up * whatever (assu$$anonymous$$g you place the rotation to point "up"). If this is along the lines of what you're looking for and you would like more clarification or a code example, I'll be more than glad to help.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KingFriggy · Jun 14, 2015 at 01:59 PM

 Public GameObject Cannon;
 
 Private Vector3 Vel;
 Private float speed = 5f;
 
 Void Start()
 {
     Vel = Cannon.transform.eulerAngles.normalize() * speed;
 }

Pass the Cannon object to the projectile. Then use the Vel variable above as the projectile velocity.

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

Firing a projectile in 2D 0 Answers

How to get rotation for projectile to fire at cursor? 1 Answer

2d Raycasting, trouble drawing rays at the correct angles 1 Answer

How to calculate angle between 2D objects and use the result? 0 Answers

problem with vector3.Angle with a fixed joint 2 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