Hey.. I don't understand why this not limit the rotation! are two months that I'm trying to do this.. hope someone can help me! thankss! Code (CSharp): void Update () { //go up if (Input.GetKey (KeyCode.Z)) { Vector3 rot = -Vector3.right * Time.deltaTime*speedUp; float rotX = Mathf.Clamp(rot.x, 0, -50f); arm[0].transform.Rotate(rot); } //go down if (Input.GetKey (KeyCode.X)) { Vector3 rot = Vector3.right * Time.deltaTime*speedUp; float rotX = Mathf.Clamp(rot.x, -50f, 0); arm[0].transform.Rotate(rot); }
You clamp rot.x to be within -50 and 0 and you store the result in the variable rotX without using it. But you are ignoring the case that rot.x could be 330°, which is identical to -30°. Instead of using Clamp only, I would use Repeat and Clamp. Like that you can make sure upfront that the angle is between 0 and 360 and than operate within this interval. WARNING: UNTESTED CODE Code (csharp): Vector3 rot = Vector3.right * Time.deltaTime*speedUp; // Make sure rotX is between 0 and 360 float rotX = Mathf.Repeat(rot.x, 360f); // Clamp it to be within 310 and 360 which is identical to -50 and 0. Mathf.Clamp(rot.x, 310f, 360f); // You forgot this line in your code! rot.x = rotX; arm[0].transform.Rotate(rot);
Thank you for your reply. I should set the clamp (loking the transform rotation in inspector) within 0 to -40 about. So i changed the values so, but the rotation is still at 360°.. (in script, "//GoUp" should go from 0 to -45 about, and "//GoDown" from -45 to 0) Code (CSharp): //go up if (Input.GetKey (KeyCode.Z)) { Vector3 rot = -Vector3.right * Time.deltaTime*speedUp; float rotX = Mathf.Repeat(rot.x, 360f); Mathf.Clamp(rot.x, 0f, -45f); rot.x = rotX; arm[0].transform.Rotate(rot); } //go down if (Input.GetKey (KeyCode.X)) { Vector3 rot = Vector3.right * Time.deltaTime*speedUp; float rotX = Mathf.Clamp(rot.x, -45f, 0); arm[0].transform.Rotate(rot); }
The code should be something like that: Code (csharp): //go up if (Input.GetKey (KeyCode.Z)) { Vector3 rot = -Vector3.right * Time.deltaTime*speedUp; float rotX = Mathf.Repeat(rot.x, 360f); Mathf.Clamp(rot.x, 360f-45f, 360f); rot.x = rotX; arm[0].transform.Rotate(rot); } //go down if (Input.GetKey (KeyCode.X)) { Vector3 rot = Vector3.right * Time.deltaTime*speedUp; float rotX = Mathf.Repeat(rot.x, 360f); Mathf.Clamp(rot.x, 360f-45f, 360f); rot.x = rotX; arm[0].transform.Rotate(rot); } If we make rotX with Repeat to be between 0 and 360, we can't Clamp it to be between 0 and -45, because it will always end up being 0 in that case. We bring rotX with Repeat to a value between 0 and 360, that means, we have to Clamp with values from the same range. That means as a consequence, we have to use 360-45 and 360. Otherwise it will not work!
If I understand your problem right, you want to limit object's rotation between minimum angle and maximum angle. If I'm right here is a simple example for you. Attach it to an object and it will limit rotation from -50 to 50. Z and X to left and right. Code (CSharp): using UnityEngine; using System.Collections; public class ClampScript : MonoBehaviour { public float speedUp = 100.0f; void Update () { if (Input.GetKey (KeyCode.Z)) { float currentAngle = transform.rotation.eulerAngles.y; while (currentAngle < -180.0f) currentAngle += 360.0f; while (currentAngle > 180.0f) currentAngle -= 360.0f; Debug.Log ("currentAngle " + currentAngle); float destinationAngle = currentAngle - Time.deltaTime * speedUp; Debug.Log ("destinationAngle before clamp " + destinationAngle); destinationAngle = Mathf.Clamp (destinationAngle, -50.0f, 50.0f); Debug.Log ("destinationAngle after clamp " + destinationAngle); float deltaAngle = destinationAngle - currentAngle; transform.Rotate (Vector3.up, deltaAngle); } if (Input.GetKey (KeyCode.X)) { float currentAngle = transform.rotation.eulerAngles.y; while (currentAngle < -180.0f) currentAngle += 360.0f; while (currentAngle > 180.0f) currentAngle -= 360.0f; Debug.Log ("currentAngle " + currentAngle); float destinationAngle = currentAngle + Time.deltaTime * speedUp; Debug.Log ("destinationAngle before clamp " + destinationAngle); destinationAngle = Mathf.Clamp (destinationAngle, -50.0f, 50.0f); Debug.Log ("destinationAngle after clamp " + destinationAngle); float deltaAngle = destinationAngle - currentAngle; transform.Rotate (Vector3.up, deltaAngle); } } } I put a lot of variables to make it easier to understand and also logging so you can watch it work. In your code you limit rotation speed which is not what you wanted to do, I believe, as you have a speedUp variable for that purpose.
This seems to work but the rotation should be up and down, this is hoizontal, so i tryied to change rotation.eulerAngles.y with rotation.eulerAngles.X but is always horizontal. Why?
Code (CSharp): transform.Rotate(Vector3.right, deltaAngle); Code (CSharp): float currentAngle = transform.rotation.eulerAngles.x; for X axis
Code (CSharp): transform.Rotate(Vector3.forward, deltaAngle); Code (CSharp): float currentAngle = transform.rotation.eulerAngles.z; for Z axis
Thank you friend work!! :') If I want increment speed when I press Z and X so that the start is not jerky, what I have to look?