- Home /
how to control player movement with ui buttons?
kinda explained it in the question, ive looked everywhere and found plenty of examples so i kinda know what im doing but not how to do it, my script right now manages keyboard keys for left right and jump through getkey "space" for jump but it uses getAxis for left and right, i want to add 3 buttons a left right and jump so what ive read i need to modify my existing scripts to contain a bool for left a bool for right and one for jump and in my button add an eventtrigger or onClick for the button and say moveRight = true or jump = true in pointer down and set it to false on pointer up but im just not sure where to add these bools or how to access them from my other script can anybody help please?
so first here is the important parts of my playerController
JUMP
if (isGrounded)
{
if (Input.GetKeyDown("space"))
{
doMovement = true;
isGrounded = false;
rb.AddForce(0, jumpSpeed, 0);
}
else if (doMovement)
{
moveHorizontal = Input.GetAxis("Horizontal");
Vector3 movement = new Vector3(moveHorizontal, 0, 0);
rb.AddForce(movement * speed);
}
WALKING
if (isGrounded)
{
Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f);
}
So i think in my buttons script for jump i can just set isGrounded for jumping and doMovement for walking but it doesnt work for some reason, i also dont know how to check if im going left or right at the moment i just check the moveHorizontal variable and update my animation to show him going the other way, like this
if (moveHorizontal > 0)
{
animator.SetFloat("rollRight", 0);
animator.SetFloat("rollLeft", 2);
jumpingDirection = true;
}
if (moveHorizontal < 0)
{
animator.SetFloat("rollLeft", 0);
animator.SetFloat("rollRight", 2);
jumpingDirection = false;
}
but i dont think i can keep doing this from another script as it would lag should i just set up a bool in here like i have jumping direction and use that from my button script? im still really new to unity so please if im talking absolute gibberish please let me know many thanks
Answer by ShadyProductions · Jan 24, 2016 at 04:54 PM
Just make a Move method and whenever u press the button u call it with your params in it.
U can parse different things into it like, if it should move left or right, or jump etc.
Here's a small idea:
void Move(string mDir) {
if (mDir == "left") {
//do moving left logic here
} else if (mDir == "right") {
//do moving right logic here
} else if (mDir == "jump") {
//do jumping logic here
}
}
im sorry i dont understand is this half an answer? are you saying this is to be saved in a separate script called move for example, and attached to each button and checked in the onClick of the button? if so how do i set myDir in my players movement script and give the value to the move script. for example my movement is
Vector3 movement = new Vector3(moveHorizontal, 0.0f, 0.0f);
i cant put that in as my moveHorizontal float changes the direction which means id have to get a reference to the script, OR am i somehow adding that move method into my existing script? sorry for my ignorance but could you elaborate @ShadyProductions
Its a method, that you can call it does things for you. $$anonymous$$aybe you should learn program$$anonymous$$g simpeler things before you try more advanced things?
If you are going to move using buttons U won't be needing to get the movedirection or anything like that? You're just gonna press Left, and the method $$anonymous$$ove("left") is called, which will know that it has to move 1 step left for example. And set the animations and other things etc.. It's all logical.
i know what a method is, and after some trial and error i now realise that i can put this method in my player movement and pass it a string from the button. and ill be using both keyboard and buttons, so i will be needing it thanks but it still doesnt quite answer how i get a left and right state from getAxis i guess ill keep checking moveHorizontal,
Your answer
Follow this Question
Related Questions
UI jump button and isgrounded using Rigidbody 1 Answer
inheritance vs multiple component for UI 1 Answer
Problem with UI buttons and MultiTouch 4 Answers
Jumping on moving platforms problem c# 1 Answer
How to get component of any button that is clicked? (Using same function for every button) 2 Answers