This feels like a brain dead question but I cannot get a solid response to this. I keep being told to not directly set transform.rotation to another quaternion because quaternions are black magic. But all other methods I can find are gradual: Quaternion.Slerp, Quaternion.RotateTowards, Quaternion.LookRotation, etc. Quaternion.Rotate rotates an object by a value, but I need to rotate an object to a value. And it needs to be done within a single frame.
Sorry for wasting your time, but I can’t find an answer to save my life.
You’ve misinterpreted someone’s advice (or gotten some very strange and bad advice), because this is just fine. Most likely you were probably being advised against working with Euler angles in code (and you shouldn’t, most of the time).
BTW, LookRotation doesn’t change rotations “gradually”, as Slerp and RotateTowards are designed to do. And even besides that, if you do this:
transform.rotation = Quaternion.Slerp(from, to, someValue);
You’re still setting transform.rotation directly to another quaternion because Quaternion.Slerp returns a Quaternion. (As do basically all of the Quaternion helper functions.)
There is nothing wrong with setting transform.rotation directly if you want an instantaneous rotation. Probably the advice you found should be saying that you shouldn’t create a Quaternion out of raw numerical values using the Quaternion constructor, because it’s very hard to wrap your head around what those numbers mean.
to rotate specific to x, y & z rotation at a certain event
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rotateObject : MonoBehaviour {
public float xAngle;
public float yAngle;
public float zAngle;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.R))//simulate a event u want to rotate during.
transform.Rotate(xAngle, yAngle, zAngle, Space.Self);//probably the worst way to show this but will get the job done
}
}
Quaternions are definitely not black magic tough and most of the time messy, but not black magic.
For your case i would recommend using Quaternion.FromToRotation(Vector3 from, Vector3 To).
In terms of the math behind them, they are a little bit black magic. But the nice thing is that there is no need whatsoever to understand the math behind them in order to use the Quaternion class.
Awesome, thanks for the response guys.