- Home /
Modify a script to use buttons instead of keys
I am using the sample assets 2d scripts for my game but my game is going to be for a mobile device, so I want buttons to control the movement. Does anybody know how to make buttons(left and right) to control the horizontal movement and a button(jump) to control the vertical movement? Here are the scripts on the player:
using UnityEngine;
public class PlatformerCharacter2D : MonoBehaviour
{
bool facingRight = true; // For determining which way the player is currently facing.
[SerializeField] float maxSpeed = 10f; // The fastest the player can travel in the x axis.
[SerializeField] float jumpForce = 400f; // Amount of force added when the player jumps.
[Range(0, 1)]
[SerializeField] float crouchSpeed = .36f; // Amount of maxSpeed applied to crouching movement. 1 = 100%
[SerializeField] bool airControl = false; // Whether or not a player can steer while jumping;
[SerializeField] LayerMask whatIsGround; // A mask determining what is ground to the character
Transform groundCheck; // A position marking where to check if the player is grounded.
float groundedRadius = .2f; // Radius of the overlap circle to determine if grounded
bool grounded = false; // Whether or not the player is grounded.
Transform ceilingCheck; // A position marking where to check for ceilings
float ceilingRadius = .01f; // Radius of the overlap circle to determine if the player can stand up
Animator anim; // Reference to the player's animator component.
void Awake()
{
// Setting up references.
groundCheck = transform.Find("GroundCheck");
ceilingCheck = transform.Find("CeilingCheck");
anim = GetComponent<Animator>();
}
void FixedUpdate()
{
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
grounded = Physics2D.OverlapCircle(groundCheck.position, groundedRadius, whatIsGround);
anim.SetBool("Ground", grounded);
// Set the vertical animation
anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
}
public void Move(float move, bool crouch, bool jump)
{
// If crouching, check to see if the character can stand up
if(!crouch && anim.GetBool("Crouch"))
{
// If the character has a ceiling preventing them from standing up, keep them crouching
if( Physics2D.OverlapCircle(ceilingCheck.position, ceilingRadius, whatIsGround))
crouch = true;
}
// Set whether or not the character is crouching in the animator
anim.SetBool("Crouch", crouch);
//only control the player if grounded or airControl is turned on
if(grounded || airControl)
{
// Reduce the speed if crouching by the crouchSpeed multiplier
move = (crouch ? move * crouchSpeed : move);
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", Mathf.Abs(move));
// Move the character
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(move > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(move < 0 && facingRight)
// ... flip the player.
Flip();
}
// If the player should jump...
if (grounded && jump) {
// Add a vertical force to the player.
anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
}
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
and
using UnityEngine;
[RequireComponent(typeof(PlatformerCharacter2D))]
public class Platformer2DUserControl : MonoBehaviour
{
private PlatformerCharacter2D character;
private bool jump;
void Awake()
{
character = GetComponent<PlatformerCharacter2D>();
}
void Update ()
{
// Read the jump input in Update so button presses aren't missed.
#if CROSS_PLATFORM_INPUT
if (CrossPlatformInput.GetButtonDown("Jump")) jump = true;
#else
if (Input.GetButtonDown("Jump")) jump = true;
#endif
}
void FixedUpdate()
{
// Read the inputs.
bool crouch = Input.GetKey(KeyCode.LeftControl);
#if CROSS_PLATFORM_INPUT
float h = CrossPlatformInput.GetAxis("Horizontal");
#else
float h = Input.GetAxis("Horizontal");
#endif
// Pass all parameters to the character control script.
character.Move( h, crouch , jump );
// Reset the jump input once it has been used.
jump = false;
}
}
Do you mean touch screen "buttons"? If yes then this link might be a good starting point: http://docs.unity3d.com/ScriptReference/Input-touches.html
It might be much easier for you to search for a touch screen controller asset and purchase that from the asset store, I dd and it helped me a lot.
Answer by Superrodan · Jan 22, 2015 at 11:59 PM
The below is just theory and how I would go about trying to accomplish this using the built in 4.6 UI.
Step 1. In whatever script handles your input, instead of the code where you detect input (Skimming through your code I believe this is an example of handling the jump button)
if (CrossPlatformInput.GetButtonDown("Jump")) jump = true;
You would want to create a bool for each input. So instead of "if(the jump button input is held down)" you want if(myNewJumpBool == true). Leave the contents of the if statement the same.
Step 2. Using the GameObject->UI menu, add a button to your scene.
Step 3. Create a simple class (call it something like "JumpButton" that has a a public reference to the script you modified before. Give the class two functions. One that sets the bool to true. One that sets the bool to false. Drag this new class on your button in the inspector.
Step 4. In your scene, click your button and hit the AddComponent button. Find "EventTrigger". Add an Event Trigger and press the Add New button. You're going to want to add a "PointerDown" and a "PointerUp". The PointerDown should refer to the script that sets the bool to true, and the PointerUp should refer to the script that sets the bool to false.
That should basically work for a simple implementation of what you're looking for.
Thanks! Do you know how I would go about doing the movement with buttons as well?
I'm not sure because I don't see in the above scripts where your input is being deter$$anonymous$$ed for the movement.