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 epn2365 · Apr 21, 2018 at 11:52 PM · unity 2dshootingbulletshmup

Ship won't shoot right in SMHUP. Help??

I'm currently working on a SHMUP/bullet hell game. I've started coding the player to shoot a laser. The problem is that every time I test the game, the laser doesn't shoot right. It shoots in the correct direction (just up) if the ship stays still or move up and down. The moment it moves either left or right, the laser curves to whichever side it moved to. Here's my code:

 public class playerFire : MonoBehaviour {
     public GameObject laserPrefab;
     public float fireDelay = 0.05f;
     float cooldownTimer = 0;
 
     void Update(){
         cooldownTimer -= Time.deltaTime;
 
         if(Input.GetButton("Fire1") && cooldownTimer <= 0){
             cooldownTimer = fireDelay;
             Instantiate (laserPrefab, transform.position, transform.rotation);
         }
     }
 }

I'd appreciate if someone could help me fix my code. The problem doesn't have anything to do with the fire delay or cooldown. This just helps with fire rate.

Comment
Add comment
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

2 Replies

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

Answer by epn2365 · Apr 22, 2018 at 03:24 AM

I found a solution.

 public class playerFire : MonoBehaviour {
     public GameObject laserPrefab;
     public GameObject shipRef;
     public float fireDelay = 0.05f;
     float cooldownTimer = 0;
 
     void Update(){
         cooldownTimer -= Time.deltaTime;
 
         if(Input.GetButton("Fire1") && cooldownTimer <= 0){
             cooldownTimer = fireDelay;
             GameObject bullet = (GameObject)Instantiate (laserPrefab);
             bullet.transform.position = shipRef.transform.position;
         }
     }
 }
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 Cherno · Apr 22, 2018 at 12:12 AM

Well, the laser projectile is instantiated so it is rotated the same way as the ship, so if the ship if tilted to the left, so is the projectile. If the laser should always shoot straight up, you would have to change it's rotation. If it's a 2D game where x is the left-right axis and y the up-down axis, you would use:

 GameObject projectile = Instantiate (laserPrefab, transform.position, Quaternion.identity);
 projectile.transform.eulerAngles = Vector3.up;


Note that transform.forward, transform.rotation etc. is always relative to the local rotation, while Vector3.up, Vector3.right etc. is in global/world space (where forward is always along the positive axis, right is always the positive x axis, etc.).

Comment
Add comment · Show 4 · 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 epn2365 · Apr 22, 2018 at 12:24 AM 0
Share

Thank you for the reply. Unfortunately, this did not solve the issue. I still had the same problem. I even checked to make sure that my ship did not tilt during movement. The moment its position on the x-axis changes to anything above or below zero, the laser curves.

avatar image Cherno epn2365 · Apr 22, 2018 at 12:29 AM 0
Share

$$anonymous$$ight be a problem with the Physics engine. How are you rotating the ship, and do you apply any force to the projectile when it is instantiated? What happens if you use Vector3.zeri as the eulerAngles of the proejctile.transform?

avatar image epn2365 Cherno · Apr 22, 2018 at 12:34 AM 0
Share

The ship does not rotate. It only moves left/right up/down. Vector.zero doesn't change anything.

Show more comments

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

81 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

velocity of bullet in rifile rotation 1 Answer

Optimizing Code 1 Answer

How should I handle masses of bullets? 1 Answer

Problem with Shooting Accuracy 0 Answers

Error in shooting script 0 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