- Home /
Problem with gradually rotation
Hi guys I have a problem with a script on rotating a controlled object with the directional arrows. I want to turn (if I press the right arrow) to a maximum velocity of 100, and if I press the left arrow I want it to -100. However, the rotation must be gradual, so at each keystroke it must increase / decrease by 1.
Much appreciated!
It should practically turn ever increasing speed. And the 100 would be the maximum at which speed can arrive, not the angle. So when speed goes from 1 to 100 this turns to the right, while it ranges from -1 to -100 goes to the left ... obviously if I leave the key the speed should stay the same. I know it is very difficult, but to better understand think a funfair ...
Answer by $$anonymous$$ · Aug 07, 2017 at 04:03 PM
public bool useInputAxis = true;
int rot;
void Update ()
{
if(useInputAxis)
{
if(Input.GetButtonDown("Horizontal") && Input.GetAxis("Horizontal") > 0)rot++;
if(Input.GetButtonDown("Horizontal") && Input.GetAxis("Horizontal") < 0)rot--;
}
else
{
if(Input.GetKeyDown(KeyCode.RightArrow))rot++;
if(Input.GetKeyDown(KeyCode.D))rot++;
if(Input.GetKeyDown(KeyCode.LeftArrow))rot--;
if(Input.GetKeyDown(KeyCode.A))rot--;
}
rot = Mathf.Clamp(rot,-100,100);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,rot,transform.localEulerAngles.z);
}
Hope It Helps.
Hello! Thank you for answering. I've tried how you said, but the object I want to turn is stuck at the push of the button and nothing happens ...
Hi, You Didn't Tell Us You Want It To Be OnHoldButton Or OnPressButton! So The Script I Gave Above Works When You Tap A Button, Not When Holding It!! So If You Want The Rotation To Change During The Hold, So Try This:
public bool useInputAxis = false;
int rot;
void Update ()
{
if(useInputAxis)
{
if(Input.GetButton("Horizontal") && Input.GetAxis("Horizontal") > 0)rot++;
if(Input.GetButton("Horizontal") && Input.GetAxis("Horizontal") < 0)rot--;
}
else
{
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))rot++;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))rot++;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))rot--;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))rot--;
}
rot = $$anonymous$$athf.Clamp(rot,-100,100);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x,rot,transform.localEulerAngles.z);
}
Cheers..
It should practically turn ever increasing speed. And the 100 would be the maximum at which speed can arrive, not the angle. So when speed goes from 1 to 100 this turns to the right, while it ranges from -1 to -100 goes to the left ... obviously if I leave the key the speed should stay the same. I know it is very difficult, but to better understand think a funfair ...
Your answer
![](https://koobas.hobune.stream/wayback/20220612132151im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Smooth Follow Camera Rotate on Z-Axis? 2 Answers
Position and rotate 3 sprites perpendicular to the edges of a triangle 1 Answer