- Home /
Hello iam working on a game wich is pretty simple in the middle of the screen is a stick wich you can tilt by using the keys a and d and now i want to replace the keys with two ui buttons.
public class RotateSkript : MonoBehaviour
{
public int speed;
void Start()
{
speed = 180;
}
void Update()
{
//GetAxis("Horizontal")
if (Mathf.Abs(Input.GetAxis("Horizontal")) > 0)
{
float turnSpeed = speed * Time.deltaTime;
float rotate = Input.GetAxis("Horizontal") * turnSpeed;
float currentRotation = gameObject.transform.rotation.eulerAngles.z;
Quaternion rotation = Quaternion.Euler(0, 0, currentRotation + rotate);
gameObject.transform.rotation = rotation;
}
}
},
Answer by tormentoarmagedoom · Feb 17, 2020 at 03:26 AM
Hello.
Now, you have all the movement inside the Update, but is only activating when GetAxis Horiz is differrnt to 0.
Unity uses this "GetAxis to detect typical Movement keys, like the arrows.
Now you will need to create your movement funcitons and assign that functions to the buttons
Learn about this, watch tutorials, try try and you will get it.
code should finish like:
void MoveToright()
{
float turnSpeed = speed * Time.deltaTime;
float rotate = 1 * turnSpeed;
float currentRotation = gameObject.transform.rotation.eulerAngles.z;
Quaternion rotation = Quaternion.Euler(0, 0, currentRotation + rotate);
gameObject.transform.rotation = rotation;
}
void MoveToright()
{
float turnSpeed = speed * Time.deltaTime;
float rotate = -1 * turnSpeed;
float currentRotation = gameObject.transform.rotation.eulerAngles.z;
Quaternion rotation = Quaternion.Euler(0, 0, currentRotation + rotate);
gameObject.transform.rotation = rotation;
}
BYEE!
Your answer
Follow this Question
Related Questions
Menu button in game isn't working 0 Answers
I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers
How to make UI buttons so my player can move along certain lines 0 Answers
I need to move a sprite when an UI button is clicked 1 Answer
Can't interact with world space ui button when cursor mode is locked any solutions? 1 Answer