- Home /
Limiting rotation in 2 axis'
I have spent many many hours working on something unsuccessfully and am just shy of given up, I wrote out this question once already and my sleeping girlfriend decided to roll over my mouse and somehow managed to click something that took me back to the sites index, deleting all I had written...
Back on track now: Basically I have lost a whole nights sleep trying to stop my toy darn car in the game i'm making flipping upside down all the blooming time (please insert profanities where seen fit) and I have had 0 success, I've tried Mathf.Clamp and Lerp and Slerp and zeroing the rotation whenever the axis rotates over a certain point and now I just want to shoot my laptop.
Anyone who can shed some light on the conclusion on this will forever be my hero... please help...
[Here's my hours worth of work, as written and unwritten with the backspace]
//logic workings set in scene
public float speed;
public Vector3 startPoint;
//In game items sourced
//private Rigidbody rb;
void Start () {
//gets the RigidBody to relate to the code
//rb = GetComponent<Rigidbody>();
}
void Update () {
//Movement control
if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
{
transform.position -= transform.forward * Time.deltaTime * speed;
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
transform.Rotate(0, -2, 0);
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
transform.Rotate(0, 2, 0);
}
}
else
{
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * Time.deltaTime * speed;
}
if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
{
transform.Rotate(0, 2, 0);
}
if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -2, 0);
}
}
//rotation control
if (transform.localRotation.x > 30 || transform.localRotation.x < -30)
{
transform.rotation = Quaternion.Euler(0, transform.rotation.y, transform.rotation.z);
}
}
Someone please save me from this hell thanks in advance - Dave
try this one to adjust in your code
void start() { var horiz = Input.GetAxis("Horizontal"); Vector3 goLeftRight = new Vector3(horiz, 0, 0);
transform.Translate(goLeftRight);
}
Unfortunately the rotation in Y and Z axis' are controlled by the rigid body so I'd need code that limited that.
I would appreciate more info about your problem, it seems very chaotic. Is game 2D? Code doesn't seem to change any other axes. Did you try setting RigidBody "Constraints", which limit the rotation that is changed by the Physics? Also, I don't know what you are going for, but the code for rotation looks like it is very sturdy - have you considered using Forces and Torques that are in RigidBody?
Answer by laurentlavigne · Apr 28, 2017 at 05:45 PM
I was going to skip that one because too much whining and girlfriend bashing but here it is, now buy her flowers toapologize, you douche.
Your answer
Follow this Question
Related Questions
moving object while it has another movement 1 Answer
gameobject still rotating over z axis 0 Answers
Mathf.Clamp acts strange when going below 0 1 Answer
2D 360 degress platformer example needed 0 Answers