Hi there. I'm having a hard time coming up with a script that will allow me to rotate an object by all three axis based on the direction I move my mouse. To clarify a little more for example: no matter what position on it's X, Y, and Z it is in, I'd like the object in to rotate downward and toward me upon moving the mouse downward, rotate leftward no matter it's X, Y, Z position if i move my mouse leftward, ETC.
Edit: I worked around this problem by using physics to my advantage: Two positions to apply force makes the object spin as if my mouse is controlling it. Here's the script. If there are no better answers in the next few days I'll close this as solved.
function Update () {
rigidbody.AddForceAtPosition(Vector3(transform.position.x+(10*Input.GetAxis("Mouse
X")),transform.position.y,transform.position.z+(10*Input.GetAxis("Mouse
Y"))),Vector3(transform.position.x,transform.position.y+4,transform.position.z));
print(transform.rotation); }
var speed : float = 1.0; //how fast the object should rotate
function Update(){
transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0) * Time.deltaTime * speed);
}
Screen.lockCursor = true;
if (Mathf.Abs(yawMouse) > 0.1f || Mathf.Abs(pitchMouse) > 0.1f)
{
targetFlyRotation = yawMouse * transform.right + pitchMouse * transform.up;
targetFlyRotation.Normalize();
targetFlyRotation *= Time.deltaTime * 3.0f;
//limit x rotation if looking too much up or down
//Log out the limitX value for this to make sense
float limitX = Quaternion.LookRotation(moveDirection + targetFlyRotation).eulerAngles.x;
//70 sets the rotation limit in the down direction
//290 sets limit for up direction
if (limitX < 90 && limitX > 70 || limitX > 270 && limitX < 290)
{
Debug.Log("restrict motion");
}
else
{
moveDirection += targetFlyRotation;
//does the actual rotation on the object if no limits are breached
transform.rotation = Quaternion.LookRotation(moveDirection);
}
}