Question by
ShadowNukePie · Aug 22, 2016 at 05:08 AM ·
rotationloops
How to make an object do a multiple tasks forever
I want to make an object rotate left (either a certain amount of degrees/for a certain amount of time, whichever is easier) then rotate right for time/degrees and have that repeat forever. How would I go about doing this?
Comment
use AnimationCurve: create animationcurve to rotate in either direction and then set Wrap$$anonymous$$ode to ping-pong.
Answer by OsmiousH · Aug 22, 2016 at 11:46 AM
Use My script
NOTE: Remember to fill in the required values in the inspector
There are clear tooltips that describes what you have to Add
//JS Javascript
#pragma strict
public enum AxisE{
x,
y,
z
}
public enum RotationE{
Right,
Left
}
@Header("INPUT-REQUIRED")
@Tooltip("The axis the rotation should be in")
public var Axis:AxisE;
@Range(0,90)
@Tooltip("The Speed the object should move in")
public var RotationSpeed : float;
@Tooltip("The Object that will rotaate")
public var RotationObject : Transform;
@Tooltip("Maximum Right value the Rotation Object can turn to in degrees")
@Range(0,360)
public var MaxRight : float;
@Tooltip("Maximum Left value the Rotation Object can turn to in degrees")
@Range(0,360)
public var MaxLeft : float;
@Header("SCRIPT DATA-DO NOT EDIT")
public var CurrentRotation : Vector3;
public var IsRotating : boolean;
public var Rotation : RotationE;
function FixedUpdate(){
CheckSetRotations();
}
function CheckSetRotations(){
CurrentRotation = RotationObject.eulerAngles;
if(Axis == AxisE.z){
RotateZ();
if(CurrentRotation.z > MaxRight){
Rotation = RotationE.Left;
}
if(-CurrentRotation.z > MaxLeft){
Rotation = RotationE.Right;
}
}
if(Axis == AxisE.y){
RotateY();
if(CurrentRotation.y > MaxRight){
Rotation = RotationE.Left;
}
if(-CurrentRotation.y > MaxLeft){
Rotation = RotationE.Right;
}
}
if(Axis == AxisE.x){
RotateX();
if(CurrentRotation.x > MaxRight){
Rotation = RotationE.Left;
}
if(-CurrentRotation.x > MaxLeft){
Rotation = RotationE.Right;
}
}
}
function RotateZ(){
IsRotating = true;
if(Rotation == RotationE.Right){
RotationObject.Rotate(0,0,RotationSpeed);
}
if(Rotation == RotationE.Left){
RotationObject.Rotate(0,0,-RotationSpeed);
}
IsRotating = false;
}
function RotateY(){
IsRotating = true;
if(Rotation == RotationE.Right){
RotationObject.Rotate(0,RotationSpeed,0);
}
if(Rotation == RotationE.Left){
RotationObject.Rotate(0,-RotationSpeed,0);
}
IsRotating = false;
}
function RotateX(){
IsRotating = true;
if(Rotation == RotationE.Right){
RotationObject.Rotate(-RotationSpeed,0,0);
}
if(Rotation == RotationE.Left){
RotationObject.Rotate(-RotationSpeed,0,0);
}
IsRotating = false;
}
OR, It is easy to create a simple animation using cool animation system
Your answer
![](https://koobas.hobune.stream/wayback/20220612091307im_/https://answers.unity.com/themes/thub/images/avi.jpg)