transform.localRotation = Quaternion.RotateTowards() or Quaternion.Euler fails on vertical axis
I would like to make straight rotations @ 0, 90,180 and 270 on each axis at the click of a button. I have created 4 buttons (up, down, right and left) and when I click each, my object rotates 90 degress to the direction chosen and then stops.
My issue is that when the object is vertical (standing up) and I click on right or left, instead of "falling" to the right/left, it rotates on its axis (it spins); however, that doesn't happen if the object is on the "floor".
Attached is the code for the buttons and the code for the Player Control.
Please help!` This is the position that gives me problems When is lying down, it is fine
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class Button : MonoBehaviour, IPointerDownHandler {
public bool clicked;
public virtual void OnPointerDown(PointerEventData ped) { clicked = true; }
}
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
private float rotSpd = 200f;
public Button btnUp, btnDown, btnRight, btnLeft;
private float rotX = 0f;
private float rotY = 0f;
private Quaternion qTo = Quaternion.identity;
void Update () {
if (btnDown.clicked) { btnDown.clicked = false; rotY += 90f;}
if (btnUp.clicked) { btnUp.clicked = false; rotY -= 90f;}
if (btnRight.clicked) { btnRight.clicked = false; rotX += 90f;}
if (btnLeft.clicked) { btnLeft.clicked = false; rotX -= 90f;}
qTo = Quaternion.Euler (rotY, rotX, 0.0f);
transform.localRotation = Quaternion.RotateTowards (transform.rotation, qTo, rotSpd * Time.deltaTime);
}
}
Answer by guilleta2000 · Feb 05, 2016 at 01:00 AM
The other problem I have is that for some reason, it auto aligns the object when the game starts and I would like it to start in whatever position it is set initially
Your answer
Follow this Question
Related Questions
Quaternion/World Local rotation problem 0 Answers
How to test for rotation? 0 Answers
GameObject Smooth Movement 0 Answers
X and Z rotating to the same direction using Quaternion.Euler 1 Answer
how to do Quaternion.RotateTowards on single LOCAL axis? 1 Answer