- Home /
Problem with rotating an object
Hi there, I have a problem with my game, I am trying to make an analog 3D clock, I am supposed to rotate a handle that usually sets the time, doing it with the mouse, but when I do that the rotation does not happen as expected, at least I do not know how to do it right, it does not rotate more than 360º, can make a full turn but back when it reaches 360º.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class rotate : MonoBehaviour
{
public float angle;
public Transform minutehand;
public Transform hourshand;
private Quaternion offset;
public float rotateregulator;
void Start()
{
}
void Update()
{
minutehand.transform.localRotation = Quaternion.Euler(new Vector3(0, rotateregulator*2, 0));
}
void OnMouseDrag()
{
float x = Input.GetAxis("Mouse X");
transform.RotateAround(transform.position, new Vector3(0, 0, 1) * Time.deltaTime*x, angle);
rotateregulator = transform.rotation.eulerAngles.x;
}
}
Below you can see an animation of what is happening:
https://drive.google.com/open?id=1dCtjMI7vLAiX72ZoSuaUhEvVuLqrv7xo
What do i need to do to make it right?
Answer by I_Am_Err00r · Aug 29, 2019 at 04:06 PM
I'm not going to lie, I'm not sure this will fix your problem, but I'd like to suggest an if statement inside of that OnMouseDrag that might do something like this:
void OnMouseDrag()
{
float x = Input.GetAxis("Mouse X");
transform.RotateAround(transform.position, new Vector3(0, 0, 1) * Time.deltaTime*x, angle);
rotateregulator = transform.rotation.eulerAngles.x;
if(rotateregulator > 360)
{
rotateregulator = 0;
}
}
I don't have access to Unity right now and have never tried something like this myself, so I don't know how that would look and if it's not a good solution, my apologies.
I'll have access to Unity about an hour and I might have something more solid if that doesn't fix it, let me know please.
Nice try, but it seems to stay the same I change this line:
$$anonymous$$utehand.transform.localRotation = Quaternion.Euler(new Vector3(0, rotateregulator, 0));
Now it do this: https://drive.google.com/open?id=1NpONIlIAE8e58QfnGk$$anonymous$$crQBgylXFULkz Starts at Y=90º and goes to Y=-90º and then goes back again
$$anonymous$$y bad, I completely forgot about this during lunch, but I left something out last time, try this:
if(rotateregulator > 360)
{
rotateregulator = 0;
transform.rotation.eulerAngles = new Vector3(0,0,1);
}
Let me know if that is any better for you, if it doesn't work I'll be able to try with Unity in about a couple hours and get back with you.
Your answer
Follow this Question
Related Questions
Clock script not working 1 Answer
Rotation changes direction for no reason? 0 Answers
Programming Accurate Clock Hands 1 Answer
Flip over an object (smooth transition) 3 Answers
Circular Rotation Input 1 Answer