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 smajo · Oct 25, 2017 at 09:34 PM · unity 5charactercontroller2d rotation

How to make a character move both vertically and horizontally on a plane?

So basically my issue is the following - my character only moves in a horizontal line, how to make it possible for it to go both ways? Here's a snippet of that part, I am stuck on the vector and the designated position.

 void FixedUpdate()
     {
         if (take_input)
         {
             float h = Input.GetAxis("Horizontal");
             
             float push;
             if (h > 0)
                 push = 1;
             else if (h < 0)
                 push = -1;
             else
                 push = 0;
             leg_anim.SetFloat("speed", Mathf.Abs(push));
             rb2d.position += my_speed * Vector2.right * push * Time.deltaTime;
 
             if (h > 0 && !facingRight)
                 Flip();
             else if (h < 0 && facingRight)
                 Flip();

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 JSierraAKAMC · Oct 25, 2017 at 10:23 PM

I'm not sure if this will work because I don't know if there's a gravitational force acting on the character or anything that is currently altering the vertical position, but you can use the current as a guide on how to do vertical movement. However, it appears that you are using a Rigidbody2D component "rb2d.position += ..." so the gravity modifier must be turned off if it isn't already, which may cause other issues.

Try this, and if it doesn't work, let me know and I'll do my best to help you further.

 float h = Input.GetAxis("Horizontal");
 float v = Input.GetAxis("Vertical"); //Get vertical input
 
 float hPush, vPush; //Horizontal and vertical push
 if ( h > 0)
      hPush = 1;
 else if (h < 0)
      hPush = -1;
 else
      hPush = 0;
 
 //Do the same for vertical
 if (v > 0)
      vPush = 1;
 else if (v < 0)
      vPush = -1;
 else
      vPush = 0;
 leg_anim.SetFloat("speed", Mathf.Abs(hPush)); //Will only be based on horizontal movement--I don't know your desired outcome
 Vector2 addPosition = my_speed * (Vector2.right * hPush) + (Vector2.up * vPush) * Time.deltaTime; //Untested, but should work
 rb2d.position += addPosition; //Separated for clarity

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

155 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

Related Questions

how to get the default third person character controller in unity 5 to move in midair 0 Answers

I'm new to Unity and C# and I need help with a character control problem. 0 Answers

RotateAround using physics 1 Answer

Why doesn't he jump as high after Idle as he does after Walking? 1 Answer

How to rotate CharacterController on slope 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