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 /
  • Help Room /
avatar image
0
Question by simplicitydown · Dec 10, 2015 at 08:05 PM · c#charactercontrollerinput.getaxis

How to use Input.GetAxis for character movement?

Hi all I have a simple character controller that moves my character in 4 directions using input.GetAxis just fine but I am having a really difficult time figuring out how to access it for use in say an if statement. I know that using GetKey I could use something like the following and then easily add more logic into it, and set up specific logic for left right up and down.

 void Update () {
     if (Input.GetKey(KeyCode.RightArrow)){
         transform.position += Vector3.right * speed;}

How would I do this using GetAxis? By the way I decided on a controller that uses input.GetAxis so that I could use a rigidbody to be able to use the physics engine later on in case anyone was wondering. Here is my relevant code, Thank you so much in advance!

public Vector2 speed = new Vector2 (50, 50); private Vector2 movement; private Rigidbody2D playerRigid;

 void Start ()
 {
     playerRigid = GetComponent<Rigidbody2D> ();
 }

 void Update ()
 {
     float inputX = Input.GetAxis ("Horizontal");
     float inputY = Input.GetAxis ("Vertical");

     movement = new Vector2 (
         speed.x * inputX,
         speed.y * inputY);

 }
 void FixedUpdate ()
 {
     playerRigid.velocity = movement;
 }





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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · Dec 10, 2015 at 08:17 PM

you mean like this?

 if (movement.x < 0f) {
     Debug.Log ("left");
 } else if (movement.x > 0f) {
     Debug.Log ("right");
 } 
         
 if (movement.y < 0f) {
     Debug.Log ("down");
 } else if (movement.y > 0f) {
     Debug.Log ("up");
 } 
 
Comment
Add comment · Show 2 · 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 simplicitydown · Dec 10, 2015 at 09:03 PM 0
Share

Brilliant! Put that into my Update function and the debugs showed up immediately no errors. Can't ask for better than that, thank you so much! <3

avatar image Jessespike simplicitydown · Dec 10, 2015 at 09:12 PM 0
Share

Cool, glad it worked out. Could you accept my answer so the question can be closed. Cheers.

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

41 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

Related Questions

My Character won't jump 0 Answers

Raycast fall detection and issues with .isGrounded, 1 Answer

Help on how to code crouching? 0 Answers

2D plaformer, Can bug me into wall 2 Answers

How to determine character move left or right reaching to final destination 0 Answers


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