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 Donnerspants · May 08, 2014 at 07:13 PM · rotationbulletgunturret2d rotation

Rotate Bullet with rotating Turret PROPERLY

Hey all,

I am making a top down 2D shooter. I want to achieve the following:

alt text

A is the base of the turret that rotates around itself. B is the cannon where the blue bullets spawn and fire from.

My problem is that I can't make the bullets rotate properly with the cannon. See picture B

alt text

I am using PoolManager to instantiate the bullet. Everything works fine except for the bullet rotation. Here is my code:

Turret.js

 var projectile       : Transform;
 var cannon          : Transform;
 private var spawnPosition : Vector3;
 
 function Start () {
     Fire();
 }
 
 function Update() {
     transform.Rotate(0, 10 * Time.deltaTime, 0); // rotate turret
 }
 
 function Fire () {
     spawnPosition = cannon.position;
     var bullet  : Transform = PoolManager.Pools["ProjectilePool"].Spawn(projectile, spawnPosition, projectile.rotation);
     
     Invoke("Fire", 2);  // invoke function every 2 seconds
 }

Bullet.js

 var speed : float = 10;
 
 function Update () {
     transform.Translate( Vector3(0,0,speed) * Time.deltaTime);
 }

I know that I shouldn't be using projectile.rotation in the Turret.js script but I can't figure out what to use instead. When I use the cannon's rotation, the bullet fires in the wrong axis.

Any help would be greatly appreciated!

Thank you!

turret.jpg (40.4 kB)
picture-b.jpg (30.9 kB)
Comment
Add comment · Show 3
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 aceX · May 08, 2014 at 07:39 PM 0
Share

Hi there, if you happen to be using a target for the turret you should possibly attach a script to the bullet prefab with Transform.LookAt(temp) where temp is = GameObject.FindWithTag("Enemy");. As I use C# I'm not sure it'll look the exact same.

Note: $$anonymous$$eep lookAt in Awake()/Start() to avoid the bullet always "looking at" the target

If it's a 2D game I have some other suggestions.

avatar image CDIEGO · May 09, 2014 at 04:13 AM 0
Share

I have the a similiar problem alt text

avatar image robertbu · May 09, 2014 at 04:14 AM 0
Share

@GDIEGO - please post as a new question. Include your code.

2 Replies

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

Answer by Sisso · May 08, 2014 at 07:15 PM

bullet.transform.rotation = turret.transform.rotation;

Comment
Add comment · Show 3 · 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 Donnerspants · May 08, 2014 at 07:28 PM 0
Share

Hey Sisso

Thanks for feedback!

I already tried this but it was no use. The projectile was spawning on the wrong axis. That's my problem

avatar image Sisso · May 09, 2014 at 12:39 PM 0
Share

Not in your current code, you are sending the prefab rotation to poolmanager. Try to send the turret rotation.

avatar image Donnerspants · May 09, 2014 at 03:02 PM 0
Share

I feel so stupid!

This worked. I had to change the rotation of my bullet projectile in the editor.

Thanks for your help man!

avatar image
0

Answer by Noob_Vulcan · May 09, 2014 at 04:47 AM

First Thing , You dont need to rotate your bullet . Bullet goes straight (thats what i think u also want).

  1. You bullet will travel in forward direction only i.e bullet.transfrom.Translate(Vector3.Froward*speed*Time.deltaTime) ..yours is also right

  2. Take another empty gameobject (i.e spawn_position) and attach it to turret nozzel and attach your bullet_spawn script to it. This gameobject will rotate when the turret rotates.

Thats it...you are good to go . :)

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

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Dart/Bullet comes out pointing up 1 Answer

Turret bullet rotation problem 1 Answer

Help with gun accuracy in degrees. 3 Answers

rotating a tank turret while the tank is rotating 3 Answers

making bullet go travel at angle fired and limit gun rotation in Unity C# version 5.2 2D 1 Answer


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