Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by brandontod97 · Mar 26, 2018 at 08:28 PM · inputaxisdebugginginput managerwasd

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;
         }
     }
 
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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();
  }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image brandontod97 · Mar 26, 2018 at 11:54 PM 0
Share

That still wouldn't prevent unity from taking the arrow keys as axis input.

avatar image Martin_Gonzalez · Mar 27, 2018 at 12:04 AM 1
Share

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

avatar image brandontod97 Martin_Gonzalez · Mar 27, 2018 at 12:23 PM 0
Share

Thank you!!!

avatar image
0

Answer by YetiMoon · Mar 27, 2018 at 03:08 PM

@brandontod97

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");
 
 }


https://docs.unity3d.com/Manual/class-InputManager.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

90 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges