- Home /
Move object while rotating another one
Hello. I am making something like old FM radio.
There is a wheel that can be rotated with mouse. Here is the code how I rotate it:
mouseClickPos = Input.mousePosition;
Vector3 dir = mouseClickPos - Camera.main.WorldToScreenPoint(transform.position);
angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
angle-=90;
angle = Mathf.Round(angle/6.0f)*6.0f;
Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
hand1.transform.rotation = Quaternion.RotateTowards(hand1.transform.rotation, q, 6);
Now there is an arrow that needs to be moved on y
axis from -4 to 4 when wheel is rotated. The arrow starts in 4, and when wheel is rotated to the right, arrow needs to go towards -4 point. And when it reaches -4, it needs to go back to 4 while wheel is rotated in same direction as from beginning. If player starts to rotate wheel to the other side (left), the arrow needs to start moving to the other side from the side it was going until then. I did try something like this but it is not working:
a1 = Mathf.Floor(oblaci.transform.localRotation.eulerAngles.z);
if(arrow.transform.localPosition.y >= 4.0f)
{
arrowUp = !arrowUp;
}
else if(arrow.transform.localPosition.y <= -4f)
{
arrowUp = !arrowUp;
}
if(a1>a2)
{
if(arrowUp)
{
arrow.transform.position = Vector3.Lerp(arrow.transform.position, new Vector3(arrow.transform.position.x, arrow.transform.position.y+0.0061111111f, arrow.transform.position.z), 1.0f);
}
else
{
arrow.transform.position = Vector3.Lerp(arrow.transform.position, new Vector3(arrow.transform.position.x, arrow.transform.position.y-0.0061111111f, arrow.transform.position.z), 1.0f);
}
}
else if(a1<a2)
{
if(arrowUp)
{
arrow.transform.position = Vector3.Lerp(arrow.transform.position, new Vector3(arrow.transform.position.x, arrow.transform.position.y-0.0061111111f, arrow.transform.position.z), 1.0f);
}
else
{
arrow.transform.position = Vector3.Lerp(arrow.transform.position, new Vector3(arrow.transform.position.x, arrow.transform.position.y+0.0061111111f, arrow.transform.position.z), 1.0f);
}
}
a2=a1;
Answer by bubzy · Oct 20, 2014 at 12:51 PM
like this ?
http://www.westwalescse.net/unity/radio/radio.html
http://www.westwalescse.net/unity/radio/ theres a zip file in here with the project.
Something like that. The problem is that when arrow reaches bottom while i am pulling mouse down, it should go up agan, not stand in bottom.
http://www.westwalescse.net/unity/radio/radio.html try this again I've changed it, although it is odd behaviour for a radio imo... :D
I said I am making "something like radio" :) Thx for your work, but it's my mistake, I explained it poorly. When arrow reaches bottom, it should start moving towards the top, not just start from top again. Here is the picture how should it go
While scrolling the wheel, arrow first goes towards bottom, then when it reaches bottom, it should start moving towards top (2.), and then again towards bottom (3.). But this example could be useful. Can I please get the zip?
Your answer
Follow this Question
Related Questions
How to Move the rotated Game Object Forward 0 Answers
Align objects based on child objects 0 Answers
Ragdoll breaks and flys far into the air... 0 Answers
[solved] Get upward orientation of object and add scale value 1 Answer
How can I defer the recalculation of child transforms whilst modifying the parent transform? 0 Answers