Hi there, i’m doing a “platform/balance/roll-a-ball” game, in wich the ground will be platforms that are tilted via the players input, and the ball moves around based on gravity.
The idea is to rotate the platforms (a Cube or Plane) in the x and z axises, so it should always be looking in the same direction.
My issues are as follow:
1.- As i rotate the platform, on the x and z axis, it starts to slightly rotate on the y axis as well, so the control starts to get more and more complicated.
2.- The platform rotation is limited to ±45° on each axis, and that works fine when i tilt in only 1 axis, when i start doing both, it goes beyond 45° and i get something like 80°.
3.- I created variables to store the rotation created by the script, but when that anomalous rotation happens, the actual X or Z rotation is beyond what the control variable has stored.
public float turnSpeed = 1.5f; // speed the platform tilts
private float maxtiltAngle = 45.0f; // max incline angle
public float vrotationTotal = 0.0f; // total rotation on the X axis
public float hrotationTotal = 0.0f; // total rotation on the Z axis
void FixedUpdate ()
{
float vrotation = turnSpeed*Input.GetAxis("Vertical"); // input vertical, rotates X axis
float hrotation = turnSpeed*Input.GetAxis("Horizontal"); // input horizontal, rotates Z axis
if (Input.GetAxis ("Horizontal") > 0.01f) // if input direction is positive
{
if (hrotationTotal <= maxtiltAngle) // and the current rotation is less than the max, it will apply rotation
{
transform.Rotate (0, 0, -hrotation); //hrotation is negative to make it go in the same direction as the arrow keys
hrotationTotal += turnSpeed*Input.GetAxis("Horizontal"); // modify the rotationTotal variable to keep updated
}
}
if (Input.GetAxis ("Horizontal") < 0.01f) // if input negative
{
if ( hrotationTotal >= -maxtiltAngle) // and the current rotation is less than the max (on the other direction), it will apply rotation
{
transform.Rotate (0, 0, -hrotation);
hrotationTotal += turnSpeed*Input.GetAxis("Horizontal");
}
}
if (Input.GetAxis ("Vertical") > 0.01f) // if input direction is positive
{
if (vrotationTotal <= maxtiltAngle) // and the current rotation is less than the max, it will apply rotation
{
transform.Rotate (vrotation, 0, 0);
vrotationTotal += turnSpeed*Input.GetAxis("Vertical");
}
}
if (Input.GetAxis ("Vertical") < 0.01f) // if input negative
{
if ( vrotationTotal >= -maxtiltAngle) // and the current rotation is less than the max (on the other direction), it will apply rotation
{
transform.Rotate (vrotation, 0, 0);
vrotationTotal += turnSpeed*Input.GetAxis("Vertical");
}
}
So basically, what i need is:
a) Is there a way to limit rotation on a specific axis so it does not happen? tried applying a rigidbody with constraints but didn’t work.
b) what would be a better way to limit the rotation angle? how can i get the actual rotation from the object so it does not exceed the limit i want to place?
Thanks in advance!!
PS: yeah, i’m reaaaally a coding noob, sorry if this is a silly question…