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
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);
}
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?
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
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.
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);
The issue is not co$$anonymous$$g from rotating a 2D object around the z axis. That's the only axis relevant for 2D
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.
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.
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.
Your answer
Follow this Question
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