- Home /
Snapping object back to default position?
I'm trying to figure out how to make my character snap back to their default position. They can only rotate, so it's just a rotation issue. I've tried MouseOnExit, MouseUp and MouseUpAsButton but nothing works. How do I fix this?
float rotSpeed = 20;
Vector3 originalPos;
private void Start()
{
originalPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y,
gameObject.transform.position.z);
}
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
private void OnMouseUpAsButton()
{
gameObject.transform.position = originalPos;
}
If I understood the question you want to reset the rotation? Why don't you save the rotation like you do for the position?
Answer by DevedGames · Sep 08, 2019 at 04:42 PM
Hey @Zeke-27, if i understood your Question right, then you just have to save the original Rotation and not the Position.
float rotSpeed = 20;
Quaternion originalRotation;
private void Start()
{
originalRotation = transform.rotation;
}
void OnMouseDrag()
{
float rotX = Input.GetAxis("Mouse X") * rotSpeed * Mathf.Deg2Rad;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed * Mathf.Deg2Rad;
transform.RotateAround(Vector3.up, -rotX);
transform.RotateAround(Vector3.right, rotY);
}
private void OnMouseUpAsButton()
{
transform.rotation = originalRotation;
}
LG Max
Thanks! Is there any way to make the transition back to the original rotation smoother?
Your answer

Follow this Question
Related Questions
Rotate object based on set time. 2 Answers
how to make a detect rotation script for negative rotation too 2 Answers
When applying a 90 degree rotation to Euler Angles, it is over/undershooting sometimes.. 2 Answers
How do I rotate an object when I press down a key and then it rotates back when I release the key 0 Answers
Translate speed keeps changing 1 Answer