- Home /
Get axis input only from certain keys
I'm new to unity so bear with me. I'm working on a simple project, where I want the player to move using only the WASD keys and not the arrow keys (as I want to bind them to other actions).
I think I'm supposed to adjust things in the input manager but I'm not sure what to edit (assuming that's where I'm supposed to look). I've tried to do some googling but I can't seem to find anything that I can understand.
Any help would be appreciated.
Here is my playerMovement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
private Animator playerAnimator;
private float moveHorizontal;
private float moveVertical;
private Vector3 movement;
private Rigidbody playerRigidBody;
private bool isOnGround = true;
[SerializeField]
private float turningSpeed = 20f;
[SerializeField]
private float speed = 5f;
[SerializeField]
private float jumpHeight = 5f;
[SerializeField]
private float teleportDistance = 5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Gather input from the keyboard
moveHorizontal = Input.GetAxisRaw ("Horizontal" );
moveVertical = Input.GetAxisRaw ( "Vertical" );
if(Input.GetKeyDown(KeyCode.Space))
{
if (isOnGround == true) {
playerRigidBody.AddForce (0, jumpHeight, 0, ForceMode.Impulse);
}
}
else if(Input.GetKeyDown(KeyCode.UpArrow))
{
playerRigidBody.MovePosition(transform.position + transform.forward * teleportDistance);
//playerRigidBody.transform.position += playerRigidBody.transform.forward * 1f;
//playerRigidBody.AddForce (transform.forward * 2f, ForceMode.Impulse);
}
else if(Input.GetKeyDown(KeyCode.DownArrow))
{
playerRigidBody.MovePosition(transform.position - transform.forward * teleportDistance);
}
else if(Input.GetKeyDown(KeyCode.LeftArrow))
{
playerRigidBody.MovePosition(transform.position - transform.right * teleportDistance);
}
else if(Input.GetKeyDown(KeyCode.RightArrow))
{
playerRigidBody.MovePosition(transform.position + transform.right * teleportDistance);
}
else{
movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
Move (moveHorizontal, moveVertical, 0);
}
}
//handles game physics
void FixedUpdate () {
//Gather the animator component and rigidbody component from the player game object
playerAnimator = GetComponent<Animator> ();
playerRigidBody = GetComponent<Rigidbody> ();
//If the movement is not equal to zero
//then set speed and animate movement
//else stop moving and play idle animation
if( movement != Vector3.zero)
{
//create a target roatation based on the movement vector
Quaternion targetRotation = Quaternion.LookRotation(movement, Vector3.up);
//create rotation that moves from current rotation to target rotation
Quaternion newRotation = Quaternion.Lerp (playerRigidBody.rotation, targetRotation, turningSpeed * Time.deltaTime);
//change the players roatation to the new incremental rotation
playerRigidBody.MoveRotation(newRotation);
//playerAnimator.SetFloat ("Speed",3f);
}
else
{
//playerAnimator.SetFloat ("Speed", 0f);
}
}
void Move (float h, float v, float u)
{
if (u < 0)
{
u = 0.0f;
}
// Set the movement vector based on the axis input.
movement.Set (h, u, v);
// Normalise the movement vector and make it proportional to the speed per second.
movement = movement.normalized * speed * Time.deltaTime;
// Move the player to it's current position plus the movement.
playerRigidBody.MovePosition (transform.position + movement);
}
//make sure u replace "floor" with your gameobject name.on which player is standing
void OnCollisionEnter(Collision other){
if(other.gameObject.CompareTag ("Ground"))
{
isOnGround = true;
}
}
//consider when character is jumping .. it will exit collision.
void OnCollisionExit(Collision other){
if(other.gameObject.CompareTag ("Ground"))
{
isOnGround = false;
}
}
}
Answer by Martin_Gonzalez · Mar 26, 2018 at 08:37 PM
You have to use the Input class in an Update.
private void Update(){
If (Input.GetKey(Keycode.W)){
MoveFoward();
}
}
That still wouldn't prevent unity from taking the arrow keys as axis input.
Go to Edit -> ProjectSettings -> Input
You will find the Axes like Horizontal and Vertical. Take out left, right, up and down strings. That should work
Answer by YetiMoon · Mar 27, 2018 at 03:08 PM
The input manager is how you should configure the majority of user input actions.
The way it works is like this; You can create new (custom) key relations and use Input.GetButton to figure out what was pressed. You might have four custom inputs.
MoveForward (w) MoveBack (s) MoveLeft (a) MoveRight (d)
You can access these like so:
void update() {
if Input.GetButton(MoveForward) {
Debug.Log ("MoveForward was pressed");
}
}
The benefit of this is you can let the user reset the keybindings. You might initially choose WASD but the user might be more comfortable with using the arrow keys - or dont allow key rebindings if you want to force the user to use WASD.
Another example is picking up items or interacting with stuff. You might choose "E" as the default but provide the user with the ability to change it to anything they want.
if Input.GetButton(PickupItem) {
Debug.Log ("PickupItem key was pressed");
}
Your answer
Follow this Question
Related Questions
Read analog stick input without using the Input Manager 1 Answer
Get Array of all Input Manager Axes 2 Answers
C# HUD axis button display 0 Answers
[New Player Input System] Separate players in scene 0 Answers
why input isn't working correctly? 1 Answer