Search Unity

How to rotate sphere character with Horizontal and Vertical Input

Discussion in 'Scripting' started by thestrandedmoose, Dec 11, 2017.

  1. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    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?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
    thestrandedmoose likes this.
  3. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    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!
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, try this:
    Code (csharp):
    1.  
    2. if(Input.GetAxis("Horizontal")) {
    3.    // your horizontal rotation code
    4.   }
    5. if(Input.GetAxis("Vertical")) {
    6.    // likewise
    7.   }
    8.  
    w,a,s,d are set to : horizontal / vertical by default in the input manager.
     
    thestrandedmoose likes this.
  5. thestrandedmoose

    thestrandedmoose

    Joined:
    Jul 21, 2015
    Posts:
    70
    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
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
    Code (csharp):
    1.  
    2. // if you ever need the axis values.
    3. float vert = Input.GetAxis("Vertical");
    4. float horz = Input.GetAxis("Horizontal");
    5. if(vert != 0 || horz != 0) {
    6.  // your code here.
    7. }
    8. // otherwise, if it's always the same:
    9. if(Input.GetAxis("Horizontal") != 0 || Input.GetAxis("Vertical") != 0) {
    10.    // your code
    11.   }
    12.  
     
    thestrandedmoose likes this.
  7. BenTWills2015

    BenTWills2015

    Joined:
    Oct 17, 2019
    Posts:
    1
  8. Deleted User

    Deleted User

    Guest

    The Roll a Ball tutorial does that...