- Home /
Rotation left & right
Hello everyone,
I don't understand why my camera can rotate on the left and not on the right, here my code :
float angle = Mathf.MoveTowardsAngle(transform.eulerAngles.y, transform.eulerAngles.y, Input.GetAxis("Mouse X")*50*Time.deltaTime);
transform.eulerAngles = new Vector3(transform.eulerAngles.x,angle, transform.eulerAngles.z);
Thank for your help
If you want to rotate it to the right, shouldnt that be a -50?
When I write -50, if the mouse go to the right -> it rotates at the left And when I write 50, if the mouse go to the left -> it rotates at the left too. But never rotates to the right :(
:/ Sorry, I think my suggestion would make something spin from clockwise to counter-clockwise. $$anonymous$$y mistake.
Answer by robertbu · Feb 22, 2013 at 04:36 AM
With the limited amount of code here, I cannot figure out what is going wrong, but I do know that anytime I deal with eulerAngles, there are issues that I have to jump through. Let me give you an alternate way to solve your problem.
If you take either a screen point or a world point and convert it to viewport point, you can tell where the point is relative to the camera view. Values greater than 0 and less than 1 are seen by the camera.
Here is a sample script. Attach it to the camera in a new scene with some object to give you feedback about the camera rotation:
public class RotateEdges : MonoBehaviour {
private Vector3 v3Rot = new Vector3(0.0f,1.0f,0.0f);
void Update () {
Vector3 v3Pos = Input.mousePosition;
v3Pos.z = 10;
v3Pos = Camera.main.ScreenToViewportPoint (v3Pos);
if (v3Pos.x < 0.1)
transform.Rotate(-v3Rot);
else if (v3Pos.x > 0.9)
transform.Rotate(v3Rot);
}
}
Note the v3Pos I calculate with a ScreenToViewportPoint() call could also be done by using a point in the scene and a WorldToViewporotPoint() call.
If this does not work the way you want it to, reply to this post with the issues, and I'll give you alternate approaches.
Thank you very much robertbu !! I just modify transform.Rotate(-v3Rot) with add the arguments "Space.World" in order to rotate using the local vaue of the object.
Have a nice day :)
Answer by Kiloblargh · Feb 20, 2013 at 12:37 AM
I don't understand why it even partially works.
You need to put the mouse input in the second argument (target) of Mathf.MoveTowardsAngle.
Right now you are moving transform.eulerAngles.y toward transform.eulerAngles.y, and that can't do any good.
Hello,
I have change my transform.eulerAngles.y by the mouse, but now I cannot go to the left or right. I upload a short video to illustrate my problem : http://www.dailymotion.com/user/Shikura95/1#video=xxnnre
Thanks
I considered your question when it was first posted, and I watched your video just now. I don't understand what needs to be rotated and to what limits. Are you just trying to rotate about the 'Y' axis based on the mouse movement?
I create a new video about my first problem: http://www.dailymotion.com/video/xxofv5_unity-rotation-probleme-2_videogames When I go to the left (with left control hold) I can turn my camera to the left, but when my mouse go to the right, it's don't turn on the right.
It's clair enough robertbu?
Clear enough. I can't look now, but I'll take a look later if someone else doesn't give you a good answer.