- Home /
Limit rotation?
Hi, I'm using this code to move by object side to side and spin it at the same time:
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
function Update () {
transform.Translate(Vector3.right * speed * Input.GetAxis ("Horizontal") * Time.deltaTime, Space.World);
transform.Rotate(-Vector3.forward * rotationSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime);
}
But I want to limit the rotation so that it only spins 30 degrees and then continues to translate. I also want to limit the translation so that it stops after 50 units. I've looked at Mathf.Clamp but can't figure out how to implement it. How can I do this? I promise you this will help me learn :)
Thanks guys, what if I want to add some damping to the rotation is that possible? eg it slows down as its about to reach its limit.
The problem with that is that it implies that the current rotation knows how fast it is going, as well as how close it is to being clamped- you might want to write your own damping algorithm, or maybe make the rotation non-linear. The Vector3 library includes a SmoothDamp function, maybe this is what you want? You would clamp the target value using the algorithms below, and then interpolate the actual value using some smoothing function.
Ok great. Just one more thing, how can I make my object rotate back to 0 degrees when the Horizontal axis is released? So the object only rotates while the left/right keys are held and then it rotates back to 0 when these keys are let go. Cheers.
function Update ()
{
if ( Input.GetAxis("Horizontal") ) {
/* place code to do stuff when the keys are held down here */
}
else {
/* no horizontal input - place code to reset rotation to zero here */
}
}
I've updated my code to include returning back to 0 degrees. If you do it only when the horizontal axis is 0, you end up getting some relatively jerky return rotation, whereas if you have your object constantly trying to rotate back towards 0, the motion will be quite smooth.
You should mark one of these answers as the correct answer, and continue on with your project. Don't use the site to write your game for you. :) (Remember, you said you wanted to learn!)
Answer by mcarriere · Sep 13, 2011 at 08:10 PM
This works, but I'll admit, it's pretty hacky:
var speed : float = 10.0;
var rotationSpeed : float = 100.0;
var rotationReturnSpeed : float = 5.0;
var initialPosition : Vector3;
var currentAngle : float;
var currentDistance : float;
function Start(){
initialPosition = transform.position;
currentDistance = 0;
currentAngle = transform.eulerAngles.z;
}
function Update () {
currentDistance += speed * Input.GetAxis ("Horizontal") * Time.deltaTime;
currentDistance = Mathf.Clamp(currentDistance, -50, 50);
var newPosition : Vector3 = initialPosition;
newPosition.x += currentDistance;
transform.Translate(newPosition - transform.position, Space.World);
currentAngle += (-1 * rotationSpeed * Input.GetAxis ("Horizontal") * Time.deltaTime);
currentAngle = Mathf.Clamp(currentAngle, -30, 30);
// this will cause the currentAngle to tend to 0.
currentAngle -= rotationReturnSpeed * Time.deltaTime * currentAngle;
transform.eulerAngles = new Vector3(0,0, currentAngle);
}
Answer by jahroy · Sep 13, 2011 at 08:37 PM
You give the clamp function three values:
The initial value you want to clamp, the min allowed value, and the max allowed value.
If the initial value is less than the min, clamp will return the min.
If the initial value is more than the max, clamp will return the max.
The initial value will be returned otherwise.
Here's one way to limit translation using the Mathf.Clamp:
var speed : float = 10.0; var rotationSpeed : float = 100.0;
var minX = -10; var minY = -10; var minZ = -10;
var maxX = 10; var maxY = 10; var maxZ = 10;
function Update () {
/* determine the translation vector using some nifty code */
var panVector = calculateTranslationVector();
/* add the translation to the current position */
var beforeClamp = transform.position + panVector;
/* clamp all three coordinates so the oject can't move too far */
var afterClamp : Vector3;
afterClamp.x = Mathf.Clamp(beforeClamp.x, minX, maxX);
afterClamp.y = Mathf.Clamp(beforeClamp.y, minY, maxY);
afterClamp.z = Mathf.Clamp(beforeClamp.z, minZ, maxZ);
/* set the transforms position to the clamped vector */
transform.position = afterClamp;
}
There's probably a function that clamps a vector, but you get the idea.
You would use the same logic to clamp a rotation.
Your answer
Follow this Question
Related Questions
Two way joint? 1 Answer
How to restrict distance from object 3 Answers
limit accelerometer controlled rotation 1 Answer
Clamp rotation of a GameObject 1 Answer