Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jimpy · Jul 08, 2020 at 01:47 PM · movement2d gamedirectiontopdown

I am having problems with making my character face the movement direction after stopping.

Ok so I'm making a 2D top down shooter game following a tutorial by Couch Ferret. When I press only press one direction, the character stays facing that direction when I let go. However when I press two buttons at once, like right and up, to move diagonally and then let go, the character faces one of the two directions but not diagonally. I guess this is because I am not letting go of both directions at exactly the same time but I still want the character to face diagonally even if I don't let go of the buttons at exactly the same time (maybe like a 0.5 second window). But I don't know how to do do it as I am still new to coding. Anyways here is my code:

     public Rigidbody2D rb;
     public float speed;
     public Animator animator;
     public Vector2 movement;
 
     void Update()
     { 
         movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
         rb.velocity = Vector2.ClampMagnitude(movement, 1.0f) * speed;
     
         if (movement != Vector2.zero)
         {
             animator.SetFloat("Horizontal", Input.GetAxis("Horizontal"));
             animator.SetFloat("Vertical", Input.GetAxis("Vertical"));
         }
        
     }

Thanks in advance for anyone who helps.

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 Chaos-Fusion · Jul 08, 2020 at 02:07 PM

IIRC, The reason the direction is not preserved is because of how the GetAxis works.

When you release one of the buttons, the value that represents that axis returns to zero. If you release 2 buttons, the last axis to equal to zero will be the direction the character is facing if that makes sense. When I worked on it, I had to create a variable that stored the values of the axis so I could preserve the direction. How you implement it will depend on what your script but this is something that I came up with though untested.

 private float xSaved, ySaved;
 
 void Update()
 { 
     //By getting the raw inputs, it only returns a 1, 0 or -1 depend on the axis direction
     float xRaw = Input.GetAxisRaw("Horizontal");
     float yRaw = Input.GetAxisRaw("Vertical");
 
     //Check if no input is being held.
     if (xRaw!=0 || yRaw!=0)
     {
         //Get the direction from the regular axis.
         xSaved = Input.GetAxis("Horizontal");
         ySaved = Input.GetAxis("Vertical");
         //Apply the preserved direction to your movement code
         movement = new Vector2(xSaved, ySaved);
         //rest of your code below here
     }
 }
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 jimpy · Jul 10, 2020 at 10:48 AM 0
Share

I don't see how this can help. Like the variable movement still has the same values as if I had never used this bit of code in the first place. Like xSaved and ySaved are still just equal to Input.GetAxis("Horizontal/Vertical") which is what my code above already does. $$anonymous$$aybe I'm just not implementing it properly though.

avatar image Chaos-Fusion jimpy · Jul 10, 2020 at 11:53 AM 0
Share

Hmm, playing around with your code, I got it running however I also realised why my code suggestion wouldn't work properly in a different way. The characters accelleration and Decelleration are based on the GetAxis Value. Using the IF statement causes the characters direction to be saved however the character will continue moving in that direction. To fix your issue, you'd probably need to have a variable to handle the speed seperately so you can use the movement variable for directional inputs. Sorry that my answer didn't help.

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

229 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 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 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

Trying to move my gameobeject in a certain direction (2D, C#) 1 Answer

I have a value that determines which way my character is facing in degrees. How do I convert that value to a range of -1 to 1 for smooth mecanim bleend tree animation? 0 Answers

Projectile changes direction and angle 1 Answer

dodge roll in mouse direction in a 2D Top down game,Top down dodge roll in mouse direction 0 Answers

2d top-down rpg direction check 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