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 /
  • Help Room /
avatar image
1
Question by TheFrozenFires · Mar 25, 2014 at 01:30 AM · 2drotationbulletbox collidermouse-look

Box Collider 2D not following object rotation

Hello,

I'm using the new 2D tools and trying to make it so I shoot a bullet in the direction of my mouse pointer. The only problem is the BoxCollider2D on the bullet does not match the angle it is travelling. Here is what I am seeing

image

From what I can tell this is being caused by the X and Y rotation changing from the way I instantiate the bullet (only the Z rotation is important for 2D correct?) Here is the code that I am using to rotate the game object from which I instantiate the bullets

 Vector3 upAxis = new Vector3(0,0,1);
 Vector3 mouseScreenPosition = Input.mousePosition;
 mouseScreenPosition.z = transform.position.z;
 Vector3 mouseWorldSpace = Camera.mainCamera.ScreenToWorldPoint(mouseScreenPosition);
 bulletSpawn.transform.LookAt(mouseWorldSpace, upAxis);


And here is the instantiation code

 if (Input.GetButton("Fire1") && Time.time > nextFireTime)
     {
         nextFireTime = Time.time + fireRate;
         Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
     }

boxcollider.png (77.4 kB)
Comment
Add comment · Show 4
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 joshpsawyer · Mar 28, 2014 at 08:22 PM 1
Share

If the BoxCollider2D is attached to the bullet and it lines up properly to begin with, then it should stay lined up regardless of how you rotate the bullet. Do the collider and bullet line up okay on the prefab?

avatar image TheFrozenFires · Mar 28, 2014 at 10:37 PM 0
Share

It lines up in the prefab. its just when the x and y rotation get changed like in the picture. If I set those rotations to 0 in play mode the collider is correct. I suppose I need a way so those two axis don't rotate

avatar image calmcarrots · May 24, 2014 at 12:14 AM 0
Share

No way....

avatar image hexagonius · Jun 20, 2017 at 07:53 AM 0
Share

To me it looks like the gameobject is rotated on the y axis by 180 degree. Don't rotate on any other axis than z. I think that happens because you're lookat is wrong. You actually don't need the conversion from the camera. If you want it to be correct however, set the z to how far the gameobject is away from the camera, not to it's position.

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by FlaSh-G · Jun 20, 2017 at 05:25 PM

only the Z rotation is important for 2D correct?

Nah, that's where the issue is coming from. Make sure to leave these axes at 0. I recommend using Quaternion.LookRotation for 2D LookAts:

 transform.rotation = Quaternion.LookRotation(Vector3.forward, lookDirection);
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 hexagonius · Jun 20, 2017 at 07:26 PM -1
Share

The issue is not co$$anonymous$$g from rotating a 2D object around the z axis. That's the only axis relevant for 2D

avatar image FlaSh-G hexagonius · Jun 20, 2017 at 08:22 PM 0
Share

Have you ever rotated a SpriteRenderer and/or a Collider2D around X or Y? This here is exactly what you get when you do that, combined with scaling. So how about you take half a $$anonymous$$ute to verify your assumption before you downvote and spread false information.

avatar image hexagonius FlaSh-G · Jun 24, 2017 at 05:01 AM 0
Share

You commented on the quote "only z is important", that it would be the reason for the problem and now you complain about my downvote saying x and y rotations are the problem.

Show more comments
avatar image
0

Answer by DeadpanDude · Jun 23, 2017 at 04:16 AM

I have the same question. I'd be glad if someone could answer it. When my 2D panels are rotated with transform.Rotate their colliders are not rotated, allowing my player character through.

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

26 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

Related Questions

Rotation animation 2d object 0 Answers

Vector2 Lerp not working when angle is 180 degrees 1 Answer

2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers

Set Instanatiated Object's Rotation Towards Player 1 Answer

How do I get my Ai agent to rotate properly according to the direction of its movement? 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