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 Neferlem · Mar 16, 2019 at 08:04 PM · scripting problemmovementplayer

How to make character move like beat'em up game ?

Hello everyone,

I'm creating a beat'em up game, and i'm stuck at movement part. I already made the first level (just a background). I'd like to know how to allow character to move in depth, like previous beat'em up games ? I did simple movement/attack/animation script, but it's only left/right/jump. How can I make player move like described in this picture ? alt text

Many thanks to anyone who can help me.

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
1
Best Answer

Answer by Ymrasu · Mar 16, 2019 at 09:17 PM

This movement is very similar to top-down 2D movement, but you want to find a different direction to move instead of straight up and down. Here is some sample code with some comments to help explain:

 // angles relative to right being 0 degrees (pos goes up, neg goes down)
 float upDownAngle = 55;
 float stairsAngle = -45;
 Vector3 upDownDirection;
 Vector3 stairsDirection;
 float speed = 10;
 bool onStairs;
 
 void Start()
 {
     // turn angles into direction vectors
     upDownDirection = new Vector3(Mathf.Cos(upDownAngle * Mathf.Deg2Rad), Mathf.Sin(upDownAngle * Mathf.Deg2Rad));
     stairsDirection = new Vector3(Mathf.Cos(stairsAngle * Mathf.Deg2Rad), Mathf.Sin(stairsAngle * Mathf.Deg2Rad));
 }
 
 void Update()
 {
     // correct for diagonal movement with normalized
     var moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;
 
     if (moveInput.y != 0) {
         // apply with the new up/down direction vector instead of Vector3.up
         transform.position += moveInput.y * upDownDirection * speed * Time.deltaTime;
     }
 
     if (moveInput.x != 0) {
         // find some way to detect if on stairs
         if (onStairs == true) {
             transform.position += moveInput.x * stairsDirection * speed * Time.deltaTime;
         } else {
             // else move normally
             transform.position += moveInput.x * Vector3.right * speed * Time.deltaTime;
         }
     }
 }

You will need to find some way to detect when you are on the stairs. This is assuming you only want to go down stairs.

Comment
Add comment · Show 5 · 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 Neferlem · Mar 17, 2019 at 06:38 PM 0
Share

$$anonymous$$any thanks @ymrasu for that exhaustive answer. Does that sample can be used "as is" ? $$anonymous$$y first 2D character controller was from basic assets pack on Unity store + an animator script I wrote for punch/kick/walk/idle animations.

avatar image Ymrasu Neferlem · Mar 17, 2019 at 06:43 PM 0
Share

Yes, I have tested the code and you can put that directly on an object to begin moving it around with WASD or the arrow keys.

avatar image Neferlem Ymrasu · Mar 17, 2019 at 07:09 PM 0
Share

It's exactly what I needed, thanks !

avatar image andrickaranda06 · May 15, 2020 at 06:23 PM 0
Share

i have a parser error in the first float

avatar image Ymrasu andrickaranda06 · May 15, 2020 at 11:14 PM 0
Share

Do you have any more information on the problem you are getting? This C# code should also be inside the class if you just copied and pasted into a new file.

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

231 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 avatar image avatar image

Related Questions

My player ball is shaking when it changes position 1 Answer

Fuel bar doesn't work! 1 Answer

My player doesn't turn based on the mouse when it's supposed to 0 Answers

,I am trying to code a dash in c#. I cant get it, help! 1 Answer

Mouse Player Movement Controller 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