How to rotate sphere character with Horizontal and Vertical Input

Hi guys,

I have a sphere within a character controller.
I want the sphere to “roll”/rotate forward when I move the WASD keys, arrow keys, or use a joystick (like a PS4 controller)

Right now, the sphere moves around with the character controller, and faces the correct direction, but does not rotate forward, giving it the impression its rolling

Does anyone know how to achieve this?

If you want it to keep rolling momentum (a la Marble Madness), then you can just AddForce to make it roll.

If you want it to still behave the same as a character controller and just look like it’s rolling, then you can use transform.Rotate(Vector3.right, - Time.deltaTime * someRotationSpeed) to roll forward, or use Vector3.forward as your axis to roll left/right.

1 Like

Thanks for your response! Yeah I was going for the second effect you mentioned.

This works to make it appear like its rotating, but I only want it to rotate when a Horizontal or Vertical input is received (like WASD, arrow keys, or joystick controller). Do you know how you would achieve this?

Thanks again!

Sure, try this:

if(Input.GetAxis("Horizontal")) {
   // your horizontal rotation code
  }
if(Input.GetAxis("Vertical")) {
   // likewise
  }

w,a,s,d are set to : horizontal / vertical by default in the input manager.

1 Like

Yeah this almost works, I got it to work with the following code. Thanks for everyone’s help!

//specify a speed to rotate the sphere
public float rotateSpeed = 360f;

void RotateSphere ()
{
if (Input.GetAxisRaw (“Vertical”) > 0) {
transform.Rotate (Vector3.right, rotateSpeed * Time.deltaTime);
} else if (Input.GetAxisRaw (“Vertical”) < 0) {
transform.Rotate (Vector3.right, rotateSpeed * Time.deltaTime);
} else if (Input.GetAxisRaw (“Horizontal”) > 0) {
transform.Rotate (Vector3.right , rotateSpeed * Time.deltaTime);
} else if (Input.GetAxisRaw (“Horizontal”) < 0) {
transform.Rotate (Vector3.right , rotateSpeed * Time.deltaTime);
}
}

void Update ()
{
RotateSphere ();
}

This will automatically apply a rotation to the sphere if any direction is pushed on the joystick, WASD keys or arrow keys are pushed. Now the only issue is that the sphere rotates at the same rate regardless of how hard you push the joystick… I’ll have to modify the above code by multiplying the rotateSpeed by a horizontal or vertical float to correct this

Sorry, yes my code was an oops :slight_smile: Glad you got it fixed up & working.

You could try a slight modification which would reduce a few lines. You seem to do the same thing no matter what direction… I wasn’t sure of that before.

// if you ever need the axis values.
float vert = Input.GetAxis("Vertical");
float horz = Input.GetAxis("Horizontal");
if(vert != 0 || horz != 0) {
 // your code here.
}
// otherwise, if it's always the same:
if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) {
   // your code
  }
1 Like

k lol

The Roll a Ball tutorial does that…