- Home /
how to rotate to face camera point in 2D
Hi all,
Just to set the scene... i'm working on a top down shooter as is the norm when starting unity :D Ive been searching many many topics going over the same thing
I've a player object, controlled with cursor keys with rigid body and collider components. this instantiates a bullet object created at the players center and then calls a bullet.getComponent.INIT() method and passes two Vector3 params namely a force and a rotation.
The force i add with AddForce is got from the normalised vector of two points drawn from the current player and the mouse pos in 3d space, then finally knocking out one of the axis. (as its 2D)
Vector3 finalforce = (transform-mousePos).normalized;
finalforce.y = 0
What i want to find is rotation.
Currently in the bullet object I'm doing:
mousePos.y = 0;
bullet.LookAt(mousePos);
but i want to optimise to not use rigid body and move the projectiles with translate and rotation. however im stuck trying to understand 3d principals of rotation. Euler vs rotation.
I come from a flash background and to create directional vectors vx = Math.sin(angle)*speed vy = Math.cos(angle)*speed and the angle is atan(diffy/diffx) angle is always an absolute one
if i try to get the angle and set it then translate position i have the projectile flying off in all different kinds of axis. I simply want this 2d and calculate the absolute angle and set the absolute rotation once on Start - the movement vector should take care of direction and speed during Update..
Any help appreciated! Thanks
Answer by robertbu · Feb 15, 2013 at 06:38 PM
If you have something that you can represent in 2D, you can use Mathf.Atan2() to convert it into an absolute rotation, and you can assign that rotation to one of the axis of your object. Your equation for directional vectors is just a polar to rectangular conversion, and works in Unity for 2D conversions. But I can't help thinking you are making this too hard.
First, is there any reason why you don't want to use LookAt()? It has nothing to do with the Rigidbody component. Second, if you just want to shoot from the forward of your character, it is common to create an empty game object at the spawn point of the bullet (typically just in front of the character or the gun with the same rotation) and make that object a child of the character or gun. Then transform.forward vector will always point the direction you want to make the bullet go. No calculation needed.
Question: Are you converting your mouse positions to world positions when you are doing your calculations?
Answer by jeffmadriaga · Feb 15, 2013 at 09:51 PM
Thank you for your feedback, much appreciated! It's all quite new to me thinking about things in 3d space, after years of 2d programming.
I'll stick to LookAt if it doesn't have anything to do with rigid body. Am i right in thinking the vector3 works because i knock out one axis (Y Set to 0, as i deal with XZ) and have a freeze rotation constraint on the Y axis of the bullet Gameobject.
Secondly the character never rotates, the game is top down imagine zelda, but has 360 degree firing (towards the mouse). I like that idea of parent-child instantiation to allow for only a toward vector, very efficient! would the bullet translation not be grouped to players movement? so when player moves left it adds the translation to the bullet making it move left?
Could you run/comment a quick example please?
The spawn point is used as the position for the bullet, but the bullet is not made a child of the spawn point, so the bullet will not swing with the camera. I posted a sample gun script here:
http://answers.unity3d.com/questions/396394/shooting-bullets-from-a-ship.html
It's not a match to your situation, but you can play with it to get an idea.