- Home /
Rotating a sphere with a joystick
Hi Unity!
So I'm working on a small game where your player is essentially stationary on a small planet, as the planet moves beneath you. I'm stumped on how to translate my joystick inputs (normalized 0-1 x and y inputs) into 3D rotation. I've tried quite a few different methods.
What I'm essentially looking for is for the ball to roll (without actually moving) towards the direction I'm pushing the joystick. For instance: From a dead stop, with an arbitrary starting rotation, if I push the joystick to the right (Horizontal input axis ==1) then the ball should spin counter-clockwise on the y axis. Likewise for the x axis for up and down, and any combination of the two.
Here's the code I've tried that I've gotten closest with:
void Update()
{
float moveHorizontal = CnInputManager.GetAxis("Horizontal");
float moveVertical = CnInputManager.GetAxis("Vertical");
//Regular rotation approach. Loses proper axis as soon as
//both X and Y are greater than (or less than) zero at the same time
transform.Rotate(moveHorizontal, moveVertical, 0);
}
Here's a diagram of what I'm looking for:
Answer by npoguca · Jul 06, 2016 at 04:53 PM
That's a piece from my app that kinda does that. Look into World and Local rotation though
float p= CrossPlatformInputManager.GetAxis("Vertical") / body.velocity.magnitude*500;
float h = CrossPlatformInputManager.GetAxis("Horizontal") / body.velocity.magnitude*500;
body.transform.Rotate(transform.forward, -h, Space.World);
body.transform.Rotate(transform.right,p,Space.World);
This wasn't exactly what I needed - but it did lead me to the solution. I ended up using :
transform.Rotate(moveVertical, moveHorizontal, 0, Space.World);
In other words, using the Space.World parameter corrected the issue. I forgot all about that overload! $$anonymous$$ostly because it doesn't prove useful that often.
Thanks!
Answer by fbecker · Jul 06, 2016 at 05:52 PM
Try:
float speed = 1f;
void Update() {
float moveHorizontal = CnInputManager.GetAxis("Horizontal");
float moveVertical = CnInputManager.GetAxis("Vertical");
//Regular rotation approach. Loses proper axis as soon as
//both X and Y are greater than (or less than) zero at the same time
transform.Rotate(new Vector3(moveHorizontal,moveVertical,0)
*speed* Time.deltaTime); }
I've add a speed value to better control the rotation.
Answer by Glurth · Jul 06, 2016 at 04:35 PM
transform.Rotate(moveHorizontal, moveVertical, 0);
This function will ADD to the transform's current rotation, about each local axis, the euler angles you provide. It is important to note that the objects local AXIS (transform.up and transform.right) will CHANGE as you do this. Calls to transform.Rotate will be around these local axis.
It sounds like you want to rotate about the WORLD Y-axis and WORLD X-axis, rather than the transform's current axis's. Perhaps something like this:
transform.rotation *= Quaternion.AngleAxis(moveHorizontal,Vector3.up); //add a rotation around world Y axis, to objects current rotation
transform.rotation *= Quaternion.AngleAxis(moveVertical,Vector3.right); //add a rotation around world X axis, to objects current rotation
Not positive, but i think this would also be equivalent to:
transform.rotation *= Quaternion.Euler(moveVertical,moveHorizontal,0);
Your answer
Follow this Question
Related Questions
Rotate Locally Player Towards Joystick/Axis Input 1 Answer
No movement to negative z-axis. 1 Answer
advanced problem for me regarding transfering rotation from the gizmo to the object 0 Answers
stay the rotation in character 1 Answer
Making my Game Object face according to direction of mobile joystick 2 Answers