- Home /
touch rotate
m on a project where i have to rotate my staring to some digress and when i reach that degree it should not go beyond that but it can be rotated to other side .i have achive some portion of that but problem is when i keep on moving my hand(either left or right ) the rotation goes above or less then my condition check and not let my code to re-move. what should i do to solve that or in simple words how can i lock the rotaion angle of my stearing to not go beond 350 and less then 208 here is my code .
if(Physics.Raycast(ray,hit))
{
euler = targetItem.transform.eulerAngles;
if((hit.transform.name=="stear") && (targetItem.transform.eulerAngles.y<350&&targetItem.transform.eulerAngles.y>208))
{
print(targetItem.transform.localEulerAngles.y);
if(Input.GetTouch(i).phase == TouchPhase.Moved )
{
targetItem.transform.Rotate(0, theTouch.deltaPosition.x *3,0,Space.World);
}
}
}
How did this pass moderation? Your explanation makes very little sense.
i got the solution for this provided by aldonaletto
// declare this variable outside any function:
private var angle: Vector3 = Vector3.zero;
...
if (hit.transform.name=="s$$anonymous$$r" )
{
if(Input.GetTouch(i).phase == TouchPhase.$$anonymous$$oved)
{
// "rotate" angle.y mathematically:
angle.y += theTouch.deltaPosition.x * rotationRate;
// clamp it to the desired limits:
angle.y = $$anonymous$$athf.Clamp(angle.y, 180, 350);
// update target actual rotation:
targetItem.transform.eulerAngles = angle;
}
}
Then please write it as an answer and accept it or close the question.
Answer by abi-kr01 · Nov 15, 2013 at 01:14 PM
this can be done using math.clamp function here is the code :-
// declare this variable outside any function:
private var angle: Vector3 = Vector3.zero;
...
if (hit.transform.name=="stear" )
{
if(Input.GetTouch(i).phase == TouchPhase.Moved)
{
// "rotate" angle.y mathematically:
angle.y += theTouch.deltaPosition.x * rotationRate;
// clamp it to the desired limits:
angle.y = Mathf.Clamp(angle.y, 180, 350);
// update target actual rotation:
targetItem.transform.eulerAngles = angle;
}
}
Your answer
Follow this Question
Related Questions
Rotate object around single axis using mouse/touch 0 Answers
Reacting to game being minimised/rotated? 0 Answers
Rotate object with touch 4 Answers
Using Touch and Drag to rotate objects. 0 Answers
Rotate Camera around Object 2 Answers