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
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.
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?
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 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
}