Limiting gameObjects rotation on Zaxis giving wierd results
I want to create a small Android game, where i am flying a plane, now the thing is i wanna limit the rotation of the plane , so that it wont go lesser than -45 and greater than 45 degree in Z axis , means when ever i take joystick to right then the plane should rotate in Zaxis and stay at 45 degree and not cross 45 degree
so directly i mean to limit the Zaxis rotation of plane between -45 and 45 degree i am using this piece of code , which is giving me pretty fair results and the gameObject is limited at 45 degree but i am having a issue that whenever the gameobject reaches at 0 the Rotation of GameObject bounces back to max Limit provided ie, maxRotation
float hortemp = CrossPlatformInputManager.GetAxis("Horizontal");
float TAngle = Mathf.Clamp(hortemp, -5, 5);
transform.Rotate(0, 0, -TAngle);
float minRotation = -45f;
float maxRotation = 45f;
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
please help me with this, how do i fix this
Answer by bhavinbhai2707 · Jul 17, 2017 at 06:50 AM
Well i did a work around with the script and it is working perfectly now what i did was gave the visuals an angle of -45 degree which is a child of another gameobject and let the parent gameobject stay at an angle of 45 so it may stay at 0 initially when game will start and clamped the max and min Angle value to 1 to 90 degree as giving min value as 0 is still creating that issue of bouncing back , so i gave min angle as 1 degree
float hortemp = CrossPlatformInputManager.GetAxis("Horizontal");
float TAngle = Mathf.Clamp(hortemp, -5, 5);
transform.Rotate(0, 0, -TAngle);
float minRotation = 1f;
float maxRotation = 90f;
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.z = Mathf.Clamp(currentRotation.z, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
This is a work around of this issue
Your answer
Follow this Question
Related Questions
How can I add inertia to a rotation when I release the mouse? 1 Answer
how do i rotate an object? C# 1 Answer
How to rotate a ship using the right analog stick? 0 Answers
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
Rotate GameObject with children around itself on mouse drag 1 Answer