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.
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
Alright the bool thing i got but not the last part
well i assume you already know how to rotate object
with example
I need it to rotate continuously at a curtain speed as long as the player holds down the button
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.
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
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?
Input.getkeydown is what I use
Edit:
I use that for shooting, input.getkey is what I use for the spinning
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
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
The two puttons i am using are UI buttons, and I have more code for the actual rotation of the player
I’m not finding anything on how to use the UI buttons in the GetKey ![]()
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:
public float rotateSpeed = 150;
void Start(){
}
void Update() {
// orbit the player every frame so it keeps looking at the centre target
float h = Input.GetAxisRaw ("Horizontal");
transform.Rotate(new Vector3(0, 0, h * rotateSpeed * Time.deltaTime));
}
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.
I think onclick works for ui buttons & mouse clicks on pc
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.
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
Please stop necro-posting. You’re replying to a 7 year old post. Simply create your own threads.