- Home /
How to transpose key press events to UI Buttons
Hi, I hope someone really helps me with my problem. I'm having an on screen buttons that controls the movement of the character, the current controls that I have is A,D and Space that controls the movement left, right and Jump of my character now I want to control my character with on screen buttons, but how do I do that with my current code? Thanks A lot in Advance to anyone replying on my question!
void Update () {
Movement();
{
float move = Input.GetAxis("Horizontal");
anim.SetFloat("speed", move);
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
}
}
void Movement()
{
if (Input.GetKey(KeyCode.D))
{
transform.Translate(Vector2.right * 3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (Input.GetKey(KeyCode.A))
{
transform.Translate(-Vector2.right * 3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true)
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
rb.freezeRotation = true;
anim.SetBool("IsJumping", false);
}
if (Input.GetKey(KeyCode.Space) && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
rb.freezeRotation = true;
jumpTimeCounter -= Time.deltaTime;
anim.SetBool("IsJumping", true);
}
else
{
isJumping = false;
}
}
if (Input.GetKeyUp(KeyCode.Space))
{
isJumping = false;
anim.SetBool("IsJumping", false);
}
}
Answer by ignacevau · Dec 25, 2018 at 03:03 PM
Since you are using GetKey()
GetKeyDown()
and GetKeyUp()
you could add an Event Trigger component to your buttons. To implement the GetKeyDown()
and GetKeyUp()
for your 'Space' button, you could write this:
public void OnSpaceDown()
{
if(isGrounded == true)
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
rb.freezeRotation = true;
anim.SetBool("IsJumping", false);
}
}
public void OnSpaceUp()
{
isJumping = false;
anim.SetBool("IsJumping", false);
}
Add these methods to the Event Trigger, for the events Pointer Down
and Pointer Up
To implement the GetKey()
you could do write:
bool RightPressed;
bool LeftPressed;
bool SpacePressed;
public void OnScreenButtonChange(string dir)
{
switch (dir)
{
case "D":
RightPressed = !RightPressed;
break;
case "A":
LeftPressed = !LeftPressed;
break;
case "Space":
SpacePressed = !SpacePressed;
break;
}
}
Add the method OnScreenButtonChange
with the respective string parameters (`"D"`, "A"
and "Space"
) to the Pointer Down
and Pointer Up
events for each button. Now you can simply replace Input.GetKey(KeyCode.Space)
with SpacePressed
in your Movement
method:
void Movement()
{
if (RightPressed)
{
transform.Translate(Vector2.right * 3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (LeftPressed)
{
transform.Translate(-Vector2.right * 3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (SpacePressed)
{
if(isJumping == true)
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
rb.freezeRotation = true;
anim.SetBool("IsJumping", false);
}
}
}
Also, Merry Christmas!
The code looks so good. The left and right button are working it moves the characters left and right but the walk animation are not playing as the character walks, and also the Idle animation and Jump animation are having a conflict they are playing at the same time? Thank you for helping me out, $$anonymous$$erry Christmas!
$$anonymous$$aybe there will be also changes on the Update method?
That's because I just showed you the method to do it, I didn't write the full script. Here is the full script:
void Update()
{
$$anonymous$$ovement();
float move = Input.GetAxis("Horizontal");
anim.SetFloat("speed", move);
isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
}
void $$anonymous$$ovement()
{
if (RightPressed)
{
transform.Translate(Vector2.right * 3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (LeftPressed)
{
transform.Translate(-Vector2.right * 3f * Time.deltaTime);
transform.eulerAngles = new Vector2(0, 0);
}
if (SpacePressed && isJumping == true)
{
if (jumpTimeCounter > 0)
{
rb.velocity = Vector2.up * jumpForce;
rb.freezeRotation = true;
jumpTimeCounter -= Time.deltaTime;
anim.SetBool("IsJumping", true);
}
else
{
isJumping = false;
}
}
}
public void OnSpaceDown()
{
if (isGrounded == true)
{
isJumping = true;
jumpTimeCounter = jumpTime;
rb.velocity = Vector2.up * jumpForce;
rb.freezeRotation = true;
anim.SetBool("IsJumping", false);
}
}
public void OnSpaceUp()
{
isJumping = false;
anim.SetBool("IsJumping", false);
}
bool RightPressed;
bool LeftPressed;
bool SpacePressed;
public void OnScreenButtonChange(string dir)
{
switch (dir)
{
case "D":
RightPressed = !RightPressed;
break;
case "A":
LeftPressed = !LeftPressed;
break;
case "Space":
SpacePressed = !SpacePressed;
break;
}
}