- Home /
How stop Z value being effected when rotating X and Y only (C Sharp)?
Hi Everyone,
I'm trying to rotate camera(X and Y only), but Z value is also being effected. Here's the script that I'm using.
if(Input.touchCount > 0){
my_Touch = Input.GetTouch(0);
if(Input.GetTouch(0).phase == TouchPhase.Moved){
Camera.main.transform.Rotate(my_Touch.deltaPosition.y * rotationSpeed, my_Touch.deltaPosition.x * rotationSpeed,0);
}
}
I also tried this but didn't work : http://answers.unity3d.com/questions/30233/problem-with-z-rotation-when-rotating-x-and-y-and.html
Could it be a problem with local and world space? What I am thinking is that you are correctly rotating it but if it is in the wrong space you will also rotate the other space Z axis.
transform.Rotate() rotates an object in local space if you don't define it. You can add an extra argument to rotate in world space if you want. If the Camera is a child of an object and that object is rotated (or gimbal lock happens like in the link you gave) and then you try rotate it like you have it will rotate relative to the object ins$$anonymous$$d of the world space. So try doing
transform.Rotate(myRotation, Space.World);
I tried it, but it didn't work. I also tried to put X rotation script to camera and Y rotation script to it's parent, but as I said Z also being effected.
Answer by jpthek9 · Mar 20, 2015 at 08:00 PM
You shouldn't use Unity's transform methods for specific behaviors like this. Instead, create a Vector3 representing a euler rotation and change it manually. Then you can do transform.rotation = Quaternion.Euler(EulerRotation).
Edit: Nevermind. transform.Rotate should work fine for your desires. Your code's correct but are you sure the Z axis is the one you don't want to rotate on?
Hi, apologize for late reply. Yes, I don't want to rotate Z axis. In my game, user can rotate and look around the world. So if Z axis got rotated then it would be hard for user to get back it default rotation.
Try playing with the rotation values yourself to see which axis rotates what. What I'm saying is that your code is correct and your Z axis rotations shouldn't at all be modified so maybe you're not defining your wanted results correctly.
Your answer
Follow this Question
Related Questions
touch input speed to camera rotate speed. scaling function needed 0 Answers
Touch Controlled Camera Inertia 1 Answer
How to smoothly rotate camera around an object with touch 1 Answer
Unity input to rotate camera 0 Answers
Touch mouseorbit 5 Answers