- Home /
Question by
risinghell09 · Jul 13, 2016 at 06:53 PM ·
rotationinputpositioningtrigonometrycircles
Moving an object in a circle towards joystick
Hey all. I've made a script that sets an object to move on a circle towards the angle of the joystick input. Here's the code for that
float h = Input.GetAxisRaw("Horizontal"); float v = Input.GetAxisRaw("Vertical");
float joyAngle = Mathf.Atan2(v, h);
float angle = Mathf.MoveTowards(angle, joyAngle, Time.deltaTime * speed);
float xPos = Mathf.Cos(angle) * radius;
float zPos = Mathf.Sin(angle) * radius;
Vector3 pos = new Vector3(xPos, transform.position.y, zPos);
transform.position = pos;
The object moves towards the angle, but it will sometimes spin all the way around the circle if the angle is moving from say 360 to 0 for example. I'm not sure how to avoid this, can anyone help out? Thanks a lot.
Comment