- Home /
Rotation problem in a Stunt Game
Hello Everyone.
I have a bike with which i want to perform a stunt i.e when bike is in air it can rotate 360 on xAxis. Problem is i have spent hours in rectifying the best possible way to do this but unable to do so. I used transform.Rotate function but it doesn't work because on xAxis it rotates from -90 to 90 then change the y-Axis and rotates again. Similarly same problems encountered when I used transform.eularAngles and in Quaternion angles as well.
Now i also worked with torque and you can rotate it to a certain degree. Please help me with this, i'm stuck with it from long time.
I am working on a stunt game. Is there is any way to work for that.
Here's my Code for Rotating Bike:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotatingStunt : MonoBehaviour {
private float currentAngleX, lastAngleX = 0, currentAngleY, currentAngleZ;
private float staticXAxis, staticYAngle;
private bool rotateLeft, rotateRight;
private BikeController bikeController;
public Rigidbody rb;
// Use this for initialization
void Start () {
rotateLeft = false;
rotateRight = false;
staticXAxis = transform.position.x;
staticYAngle = 0;
rb = gameObject.GetComponent<Rigidbody>();
}
void FindBikeController() {
bikeController = gameObject.GetComponent<BikeController >();
}
// Update is called once per frame
void FixedUpdate () {
if(!bikeController)
FindBikeController();
if(rotateRight && bikeController.speedOfBike != 0) { // Rotating Bike to Right Side
rb.AddTorque (transform.right * 1000f * Time.fixedDeltaTime, ForceMode.Acceleration);
// Quaternion target = Quaternion.Euler(currentAngleX, 0, 0);
// // Dampen towards the target rotation
// transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * 5.0f);
// rb.AddTorque(10,0,0);
// currentAngleX += 150 * Time.deltaTime;
// transform.eulerAngles = new Vector3(currentAngleX, currentAngleY, currentAngleZ);
// transform.rotation = Quaternion.Euler(new Vector3(currentAngleX, currentAngleY, currentAngleZ));
// print("Right = " + transform.rotation);
} else if(rotateLeft && bikeController.speedOfBike != 0) { // Rotating Bike to Left Side
rb.AddTorque(-transform.right * 1000f * Time.fixedDeltaTime, ForceMode.Acceleration);
// currentAngleX -= 150 * Time.deltaTime;
// Quaternion localRotation = Quaternion.Euler(currentAngleX - lastAngleX, 0, 0);
// transform.rotation = transform.rotation * localRotation;
// transform.eulerAngles = new Vector3(currentAngleX, currentAngleY, currentAngleZ);
// print("Left = " + transform.rotation);
}
currentAngleZ = 0;
currentAngleX = transform.eulerAngles.x;
currentAngleZ = transform.eulerAngles.z;
if(!(transform.position.x < (staticXAxis + 0.01) && transform.position.x > (staticXAxis - 0.01))) // Trying to put bike on xAxis as bike will move straight
transform.position = new Vector3(staticXAxis, transform.position.y, transform.position.z);
if(staticYAngle != transform.eulerAngles.y) { // Trying to put bike on original yAxis if bike turn on wrong side while rotating
currentAngleY = staticYAngle;
transform.eulerAngles = new Vector3(currentAngleX, staticYAngle, currentAngleZ);
}
// transform.eulerAngles = new Vector3(currentAngleX, currentAngleY, currentAngleZ);
// }
// print("Left = " + transform.eulerAngles.x);
}
public void RotatePlayerToLeft() {
rotateLeft = true;
rotateRight = false;
currentAngleX = transform.eulerAngles.x;
currentAngleY = transform.eulerAngles.y;
currentAngleZ = transform.eulerAngles.z;
}
public void RotatePlayerToRight() {
rotateRight = true;
rotateLeft = false;
currentAngleX = transform.eulerAngles.x;
currentAngleY = transform.eulerAngles.y;
currentAngleZ = transform.eulerAngles.z;
}
public void DontRotate() {
rotateLeft = false;
rotateRight = false;
}
}
Through Buttons i'm calling functions for rotating left or right and in update applied torque.
Looking forward for your response.
Thanks in Advance
Could you please post the code, so I can see if I can help.
@NathanGG Thank you for your response. I have edited my question and added code in it.
Hi @c_Feng can you please help me with this. Got Stuck in it from a long time. Thanks
Your Bike's forward axis isn't Z? How can you rotate right by using rb.AddTorque (transform.right)? Shouldn't it be transform.up? It may sound like an off-topic question, but if your forward axis is not Z then you have to tell us.
AddTorque (transform.right) would usually pitch your object downwards (meaning clockwise if the object is viewed from its right side).