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 Morty123BA · Jul 06, 2020 at 01:21 PM · laser beam

laserbeam on a 90 degree angle

Hi, Im new to Unity and scripting. My problem is my spaceship which shoots the laser alt textbeam with an 90 degree angle. This is the code.

             GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation);
              newBullet.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * bulletForce);
              Destroy(newBullet, 2.0f);
              currentAmmoState--; 

There is no cote on the laser beam itself. Best Regards.

laser-beam-ref.jpg (165.7 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 Miaw666 · Jul 06, 2020 at 04:22 PM 0
Share

Welp, I'm also not that good with Unity2D, but I'll try to help: I think the problem is with the "GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation);". You're saying "spawn" a laser with the same rotation as the ship? (If the script is attached to the ship". $$anonymous$$aybe, the sprite of the ship doesn't match the ship's gameObject's rotation. I mean, your code seems right to me, but maybe the "front" of the ship sprite is wrong. If the laser is a sprite, and not a line renderer, maybe the sprite of the laser is wrong, not the ship. Is the sprite of the ship in the middle of the pivot? and so on. I can try to help, but I would need to see the "blue, green, red arrows of the gameObjects". (I'm kinda dumb, I can't really tell what's going on in the picture ;( )

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Captain_Pineapple · Jul 06, 2020 at 05:40 PM

Basically what @Miaw666 said was correct. The "bullet" you instantiate is just not oriented correctly.


simply rotate it by 90 degrees by using newBullet.transform.Rotate(90f, 0f,0f); The angle might need a - and/or you might have to set the "90" in a different axis of the rotation function as i don't know which axis you have to rotate about. That detail is missing in the post. Be aware here that this will also change the Vector you have to use for the force you apply so the Vector2.up will probably have to be something like Vector2.forward.

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 Pro-Ton · Jul 06, 2020 at 07:27 PM

Just an addition to @Captain_Pineapple 's answer, if your game is 2D, you need to rotate it by 90 degrees on the z-axis, and in that case, Vector2.up will work fine. Another way to rotate this is by changing the 1st line to

 GameObject newBullet = Instantiate(bullet, transform.position, transform.rotation * Quaternion.Eular(0,0,90));  // to rotate it 90 degree more from the initial 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 Miaw666 · Jul 06, 2020 at 07:39 PM 0
Share

Why times? Shouldn't it be plus?

avatar image Eno-Khaon Miaw666 · Jul 06, 2020 at 08:15 PM 0
Share

Concatenating Quaternions is done using noncommutative multiplication.

In other words, you apply each in turn by multiplying them, and you get a different result depending on which comes first in the series.

 Quaternion a, b;
 (a * b) != (b * a);


The reason the order matters can be described with an example of two 90-degree rotations (provided based on thumb/index finger as models):

 Example: rotate 90 degrees right, then 90 degrees forward
 1: forward is now right, up is now up
 2: forward is now right, up is now forward

 Example: rotate 90 degrees forward, then 90 degrees right
 1: forward is now down, up is now forward
 2: forward is now down, up is now right

avatar image Miaw666 Eno-Khaon · Jul 06, 2020 at 08:26 PM 0
Share

Holy shirt... I didn't expect it to be this complex. But I get what you mean, thanks for the lesson :)

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

132 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 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

Line Renderer & Raycasting SOS !!! 1 Answer

why can't Line Renderer catch movement of camera? 0 Answers

GVR laser pointer maximum distance not changing 0 Answers

coroutine(Firelaser)couldn't be started 2 Answers

How to make a laser particle? 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