- Home /
Unity 2D Rotate Problem!
Hello Everyone I'm Trying To Make 2D Shooting Game
but i just need a little help in rotating
i Use This Code
private var mousePos : Vector2;
private var screenPos : Vector3;
function Update () {
mousePos = Input.mousePosition;
screenPos = Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, transform.position.z - Camera.main.transform.position.z));
transform.rotation.eulerAngles.z = Mathf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x))*Mathf.Rad2Deg;
}
To Rotate The Gun.
but the problem that i need To Rotate The Gun 180 Degree Only in Right Side
if i did 181 Degree or More. i want to rotate The Whole Body to other side.
I'm trying to figure out what you are asking here. $$anonymous$$y best guess is this:
That is when the cursor is place as on the left panel, the gun has this orientation, and when the cursor is as on the right panel, the gun is flipped.
If this is what you want, it might be easier to have two textures and flip the textures for each angle rather than write rotation code to do the filp. Plus you have the added complication in a flip that planes are one sided.
not exactly
when cursor is on the right panel then moved to left panel
i want the whole character to rotate to the left panel not only the weapon
sorry for my bad English
Answer by robertbu · Aug 07, 2013 at 05:39 PM
The easy way to do this is to use a empty game object with the visible object as a child. You would then detect the left/right and do whatever flip you wanted to the child.
dir = screenPos - transform.position;
if (Vector3.Dot(dir, Vector3.right) > 0) {
child.localRotation = Quaternion.Euler(0,0,0);
}
else {
child.localRotation = Quaternion.Euler(0,180,0);
}
The rotations here are just placeholders. You will need to figure out what rotation you want in the inspector and then fill in the correct values. I would be more efficient to precalculate the rotations in Start() and then just assign the Quaternion in Update().
Start() {
qLeft = Quaternion.Euler(0,180,0);
qRight = Quaternion.Identity;
}
Sorry, but i Didnt Get it ..
here is the CodE :
private var mousePos : Vector2;
private var screenPos : Vector3;
public var child : Transform;
function Update () {
mousePos = Input.mousePosition;
screenPos = Camera.main.ScreenToWorldPoint(Vector3(mousePos.x, mousePos.y, transform.position.z - Camera.main.transform.position.z));
transform.rotation.eulerAngles.z = $$anonymous$$athf.Atan2((screenPos.y - transform.position.y), (screenPos.x - transform.position.x))*$$anonymous$$athf.Rad2Deg;
var dir = screenPos - transform.position;
if (Vector3.Dot(dir, Vector3.right) > 0) {
child.localRotation = Quaternion.Euler(0,0,0);
}
else {
child.localRotation = Quaternion.Euler(0,180,0);
}
}
and this Script is attached to the Weapon Transform[Inside The Player] what is the child then?
anyway this is what happend when i added your code
As I mentioned in the answer, the values inside the Euler() were just placeholders. Play with other values until it works the way you want. With the gun pointed left, change the rotation in the inspector until you get what you want. Then use that value in the Euler() call in the code above. If something is going to work then it will be something like (180,0,0) or (0,0,180), or (90,180,0).
Note I suspect given the 2D and 3D nature of your problem, that this form of rotation might not give you what you want...that there will be no Euler() values that display the gun and the box correctly. This is especially true given that planes are one sided. That is why I suggested changing the texture rather than complex rotations to start with. If your problem can be solved by rotating, then some value in the Euler() call will work.
When you tossed in the box, I was confused about how you constructed your game. I don't know the rotational relationship between the two, nor how you defined your pivot. Just guessing, I think all you need to do is flip the texture as the cursor crosses the boundary. The can flip the texture by changing the tiling/offset. You can use $$anonymous$$aterial.SetTextureScale(). The default scale is (1,1). Change either value to -1 to filp. You can use the same code above that calculates the Dot product to select left and right.
okey, forgot about the Box can u give me code to flip the Weapon Horizontal?
i tried SetTextureScale , didnt work well for me
Your answer
Follow this Question
Related Questions
[Help] Object flips around when using transform.Rotate 3 Answers
Rotating a 2D GameObject on Z regardless of the distance. 2 Answers
problem with rotation 2D 1 Answer
How can I rotate my player to look at the direction my Joystick is pointing to? (Top-Down 2D) 3 Answers
Function to rotate my Player smoothly taking the y and use it to have angle 1 Answer