- Home /
The question is answered, right answer was accepted
Y Axis Limit
Hello everyone , i have a an object and i want to do limit to y axis im using javascript. How can i make it in 5-17 range ?
Thanks.
There's quite a few ways to do it, which one works best actually depends on how you access that object's y value. The easiest but least elegant way would be to have your upper and lower range limit and use $$anonymous$$athf.Clamp in Update. Another option would be using a property with a range attribute, and each time you change your object's y value you actually access that property, which handles setting the y value. This only works if you don't utilise physics, though.
Answer by christoph_r · Sep 28, 2014 at 05:19 PM
How about this?
function Update ()
{
if (CFInput.GetKey(KeyCode.W) && transform.position.y <15)
{
rigidbody.velocity.y = speed;
}
else if (CFInput.GetKey(KeyCode.S) && transform.position.y > 7)
{
rigidbody.velocity.y = speed *-1;
}
else
{
rigidbody.velocity.y = 0;
}
}
Answer by robertbu · Sep 28, 2014 at 01:53 PM
If your object is only rotating on the 'y' axis, you can do:
var euler = transform.eulerAngles;
euler.y = Mathf.Clamp(euler.y, 5.0, 17.0);
transform.eulerAngles = euler;
The problem is significantly more complicated if you are allowing rotation on multiple axes. I'd need to see your rotation code in order to advise.
Sorry but i don't want to rotate this object i just want to do limit to y direction this is my y speed code ;
#pragma strict
var moveUp : $$anonymous$$eyCode;
var moveDown : $$anonymous$$eyCode;
var speed : float = 5;
function Update ()
{
if (CFInput.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
rigidbody.velocity.y = speed;
}
else if (CFInput.Get$$anonymous$$ey($$anonymous$$eyCode.S))
{
rigidbody.velocity.y = speed *-1;
}
else
{
rigidbody.velocity.y = 0;
}
}
Follow this Question
Related Questions
Rotate and Move towards Mouse Point 0 Answers
How to lock the y axis in camera script? 2 Answers
LookAt without Y axis 2 Answers
Lock the Camera's Y Rotation Axis? 0 Answers