- Home /
How to simulate Arrow Keys Input
I want to simulate Arrow Keys left clicking objects(one for left arrow and another for right arrow) to use Horizontal Input, allowing me to move my platform character
I'm using two scripts, one to get the input and another to move and etc. (Tried to unite them but failed...) Here they are:
First:
using UnityEngine;
[RequireComponent(typeof(PlatformerCharacter2D))] public class Platformer2DUserControl : $$anonymous$$onoBehaviour { private PlatformerCharacter2D character;
void Awake()
{
character = GetComponent<PlatformerCharacter2D>();
}
void Update ()
{
}
void FixedUpdate()
{
float h = Input.GetAxis("Horizontal");
// Pass all parameters to the character control script.
character.$$anonymous$$ove(h);
}
}
Second:
using UnityEngine;
public class PlatformerCharacter2D : $$anonymous$$onoBehaviour { bool facingRight = true; // For deter$$anonymous$$ing which way the player is currently facing.
[SerializeField] float maxSpeed = 10f; // The fastest the player can travel in the x axis.
[SerializeField] Layer$$anonymous$$ask whatIsGround; // A mask deter$$anonymous$$ing 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 deter$$anonymous$$e if grounded
bool grounded = false; // Whether or not the player is grounded.
Animator anim; // Reference to the player's animator component.
AnimatorStateInfo currentState;
void Awake()
{
// Setting up references.
groundCheck = transform.Find("GroundCheck");
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);
}
public void $$anonymous$$ove(float move)
{
//only control the player if grounded or airControl is turned on
if(grounded)
{
// The Speed animator parameter is set to the absolute value of the horizontal input.
anim.SetFloat("Speed", $$anonymous$$athf.Abs(move));
// $$anonymous$$ove the character
if(anim.GetCurrentAnimatorStateInfo(0).nameHash == Animator.StringToHash("Base Layer.Idle")){
maxSpeed = 10;
}
else if(anim.GetCurrentAnimatorStateInfo(0).nameHash == Animator.StringToHash("Base Layer.Walk")){
maxSpeed = 10;
}
else {maxSpeed = 5;}
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();
}
}
void Flip ()
{
// Switch the way the player is labelled as facing.
facingRight = !facingRight;
// $$anonymous$$ultiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
As you can see, the first script is getting the inputs from Horizontal, but I want to make it get them from two "buttons", so, for example, when i click the left button it will move the character to left...
Do you want a gradual accelerate/decelerate as you press/release? Because that's what using the Vert/Horiz axis does.
If you don't want that, you can just read directly from any keys you want. Should be able to find code-snippets doing that (`Input.get$$anonymous$$ey`.)
Your answer
Follow this Question
Related Questions
OnGUI() and key pressing problems. 2 Answers
menu issue and mouse click 0 Answers
changing from mouse click to keys 0 Answers
turn off script with a key.. 1 Answer
Unity 4.6.3f1 bug or normal behaviour? MouseButtonDown cancelled/ ignored by key press 0 Answers