Search Unity

Programming two buttons to rotate player

Discussion in 'Scripting' started by JPF55, Aug 5, 2015.

Thread Status:
Not open for further replies.
  1. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Hello,
    I am stuck on programming two buttons to rotate a player, the left button to rotate the player clockwise on the X axis and a right button to rotate the player counterclockwise on the X axis. Can anyone help me figure this out? The two buttons on buttons you can make in the new UI system.
     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    use flags to check if button is pressed (bool) and set to false when player realeses button. about rotation add it to update (or coorutine) as lerp rotation
     
  3. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    Alright the bool thing i got but not the last part
     
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
  5. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    I need it to rotate continuously at a curtain speed as long as the player holds down the button
     
  6. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    or, maybe if its easier for you make another flag. while button is pressed and flag2 active rotate for small amount (0.2 or something), deactivate flag2 and activate it after small amount of time like half a second or 0.1s. way easier and you can smooth things out by reducing time to reactivate flag2 and reducing rotating angle. depends how "smooth" you want it.
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Just map one button to negative rotation & the other to positive rotation
    Sorry for pseudo code
    Float direction;
    If left arrow, direction = -1;
    If right arrow, direction =1;

    Rotation = direction * rotation speed* time.deltatime

    Then plug the Rotation into your turn code.
    Or something like that. This is what I did to make a ship rotate in space
     
  8. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    This might just work, I already have code to turn the player on the keyboard now if I can just figure out how to set up to check if the button is pressed then I could put that in the FixedUpdate section before the transform position and it should work. Do you know how to check if the button is pressed?
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Input.getkeydown is what I use

    Edit:
    I use that for shooting, input.getkey is what I use for the spinning
     
    Last edited: Aug 6, 2015
  10. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40

    So i have this right now;

    if (Input.GetKey ("leftButton"))
    rotatePlayer = rotatePlayer - 1.of;
    if (Input.GetKey ("rightButton"))
    rotatePlayer = rotatePlayer + 1.of;

    how do I connect the two buttons to these?

    Edit: Looking over the Input Manual so if you know give me a sec and see if i can find it
     
  11. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    It should have prompted you. From memory (no PC ATM) for c#
    Input.GetKey(keycode.'whatever key you want, it will autocomplete")

    & I think you want to multiply those 1 values
     
  12. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    The two puttons i am using are UI buttons, and I have more code for the actual rotation of the player
     
  13. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    I'm not finding anything on how to use the UI buttons in the GetKey :(
     
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    sorry, I don't use UI buttons, I'm still learning on pc games. Maybe you could make the UI buttons public gameobjects on the script & drag them in the inspector then reference them in the rotation script? What I had for my keyboard rotation code was:

    Code (CSharp):
    1. public float rotateSpeed = 150;
    2.        
    3.     void Start(){
    4.     }
    5.        
    6.         void Update() {
    7.             // orbit the player every frame so it keeps looking at the centre target
    8.         float h = Input.GetAxisRaw ("Horizontal");
    9.         transform.Rotate(new Vector3(0, 0, h * rotateSpeed * Time.deltaTime));
    10.  
    11.         }
     
  15. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    I can seem to figure out how to use the UI buttons, I have everything working for PC but i need it to work on phones, which is where the UI buttons come in.
     
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I think onclick works for ui buttons & mouse clicks on pc
     
  17. JPF55

    JPF55

    Joined:
    Feb 26, 2015
    Posts:
    40
    I found out how to do it, pointerDown and pointerUp worked, I found a video online that showed step by step how to set it up. Thank you to everyone who helped me.
     
  18. rajababricz

    rajababricz

    Joined:
    Aug 23, 2022
    Posts:
    2
    pls can you send me a link of the video to or can you show me how you did it with the ui button i really want to know i have a mobile car game to and i want a fuction like this so pls tell me how you did it
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Please stop necro-posting. You're replying to a 7 year old post. Simply create your own threads.
     
Thread Status:
Not open for further replies.