I have a sphere and I have a problem finishing spinning it with drag. How do I save and assign that new rotation value to zero without the sphere returning to the initial rotation? since I want that new position to be assigned values of zero to start the rotation again from where the sphere stayed when lifting the finger with that new position but with initial values of zero to rotate correctly since with my code when lifting the finger or start a new drag starts with the values from scratch and removing the part of OnMouseUp uses the previous values where the finger is lifted and begins to rotate erroneously.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotateSphere : MonoBehaviour
{
float rotationSpeed = 5f;
void OnMouseDrag()
{
float XaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;
float YaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
// select the axis by which you want to rotate the GameObject
transform.Rotate(Vector3.down, XaxisRotation);
transform.Rotate(Vector3.right, YaxisRotation);
}
void OnMouseUp()
{
this.transform.localRotation = Quaternion.Euler(0f, 0f, 0f);
}
}
Hi @Alucard-Belmont, I don’t quite understand what you want to do, but here is how you can save a rotation of an object to apply later (or to another object), and how to reset all axes rotations to zero.
-
to save a rotation, create a new Quaternion and set it equal to some object’s transform.rotation for example
Quaternion savedRotation = gameObject1.transform.rotation;
you can do the reverse and set any object rotation to that, say if you want to copy one object’s rotation to another.
- to reset (zero out) a rotation, just set the object’s rotation equal to Quaternion.Identity
Easy peasy!
@streeetwalker I managed to get it. All he needed was to spin it like a globe. After several things that I had to do, I finally had time to continue with my project and when looking for solutions I made it work. The code is the following:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RotationSphere : MonoBehaviour
{
public float rotationSpeed = 3f;
void OnMouseDrag()
{
float XaxisRotation = Input.GetAxis("Mouse X") * rotationSpeed;
float YaxisRotation = Input.GetAxis("Mouse Y") * rotationSpeed;
transform.Rotate(Vector3.up, - XaxisRotation * rotationSpeed, Space.World);
transform.Rotate(Vector3.right, YaxisRotation * rotationSpeed, Space.World);
}
}