- Home /
"Rotate toward" in 2D
Hi,
so am trying to make a simple 2D shooter game (a clone of a flash game i made a year ago ) until now, and thanx to the unity community help, i was able to make the ship move where i click, but for the rotation am stacked again, i tried to do something like this :
Vector3 lookDir = transform.position - targetPosition;
lookDir.y = 0;
transform.rotation = Quaternion.LookRotation (lookDir);
but the rotation is tottally messed up, i tried to set the x to 0 then the z to 0, but always no luck doing that
thank you
Solving these problems depends on how you setup your model. The look functions like LooAt() and LookRotation() depend on the forward of the model being pointed at positive 'z' when the rotation is (0,0,0). It's best to fix this in a 3D modeling program, but you can get around by using an empty game object and making the visible ship a child of the empty object. The script goes on the empty game object.
You could simply use transform.LookAt(targetPosition); This way it doesn't matter how your.. "gameplay"-plane is set up. (orientation, offset,...)
@boni - it does matter. LookAt() points the positive 'Z' of the object towards the specified point. If the model does not have the front of the ship point at positive 'z', then the rotation will not be correct. His calculation does the exact same thing as LookAt().
@robertbu - how do i know the forwoard of the model ? and where is he pointed at ? and could you please explain more about the empty object solution ? just one last thing, is there any place where developers with 2D background can start with unity3D ? cause i am really confused
Answer by robertbu · Aug 08, 2013 at 05:27 PM
Take your model in the scene, set the rotation to (0,0,0). The side facing positive 'Z' is forward. In a new scene the default camera setup looks at the back of objects in the Game view when rotation is (0,0,0). BTW your code looks fine if the model was setup correctly.
To implement the empty game object soution:
Place your model at (0,0,0)
Create an place an empty game object at (0,0,0) with rotation (0,0,0)
Make the model a child of the empty game object by dragging the model on top of the empty game object in the Hierarchy view.
Rotate the model (now a child) intil the front of the model is facing positive 'Z'.
Remove the script you had above from the model and place it on the empty game object.
There are other ways of setup this up and/or solving this rotation problem. Again the best way is to fix it in the modeling program.
am sorry but it didn't work (i think i didn't fully understand) Question : when i start the game and let object move, then when i change the rotation of Z axis in the inspector, i got the exact result am looking at, can't i just transform that into codes ?
First, the fix above will work. But yes you can just write code to fix it given the right information. For me to suggest a fix for your specific setup I need to know two things:
What is the inital rotation of your object (I assume it is a plane)?
What side of the plane (usually top or right), do you want to do the pointing towoards the target?
I'd like to do a quick test of any specific solution before I give it to you, and it will be a couple of hours before I'm back at a computer with Unity installed.
the object rotation is X:-90,Y:0,Z:0 and i honestly don't know what side am using :p and yes i wanted to point towards the target (not immediately) like here it's a basic "car" movement with the mouse nothing more, if you needed more information, here is the whole project here it's less than 1mo, i'll be very happy if you fix it there with some comments in the scripts, cause finding some good 2D reference for unity is pretty hard (maybe am looking at it from the wrong side) thank you very much
You will find a package with two solutions to your problem here. The first solution is the child game object as outlined above. For the second solution:
I used the CreatePlane editor script from the Wiki to create a new plane that faces the camera (vertical) when the rotation is (0,0,0).
I made a new material with the arrow facing right when the rotation is (0,0,0).
I duplicated your script and change the rotation code for this new plane. The new rotation for this plane is:
if (targetPosition != transform.position) { Vector3 lookPos = targetPosition - transform.position; float angle = $$anonymous$$athf.Atan2(lookPos.y, lookPos.x) * $$anonymous$$athf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); }
Thank you very much !!! this is really helpful !! there is a little bug when the cursor is over the plan, it rotates completely wrong but i'll try to find a way to fix that, thank you again !! EDIT: the bug i talk about exist only in my scene, the "CreatePlan" and "child" solutions works perfectly fine again thank you so much
Your answer