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 /
This question was closed Jul 09, 2018 at 05:28 AM by bilginba for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by bilginba · Jul 04, 2018 at 06:00 AM · 2d gameplayer movementcollision2d

How to move player like pacman in 2d? How to prevent coliision hit when played turned the corner?

Hello

I'm sorry if my question is a bit unclear. I managed to give my player movements like pac-man yet if i want to turn the corner i want to make my player slide smoothly through the maze. However, with my movement script most of the time it hits the corner of the objects and can't go between the objects . Also in my game can i make the distance of the objects as my player objects size? I want it to go between two objects and still move smoothly.

Sorry I asked a bit too much but I really couldn't solve it. Thank you.

My Movement script:

 void FixedUpdate() {
 
          //Movement
          float h = Input.GetAxisRaw("Horizontal");
          float v = Input.GetAxisRaw("Vertical");
          if (Input.GetAxisRaw("Horizontal") > 0f || Input.GetAxisRaw("Horizontal") < 0f) {
              GetComponent<Rigidbody2D>().velocity = new Vector2(h, 0) * speed;
          }
          else if (Input.GetAxisRaw("Vertical") > 0f || Input.GetAxisRaw("Vertical") < 0f) {
              GetComponent<Rigidbody2D>().velocity = new Vector2(0, v) * speed;
          }
  
      }
  
      void OnCollisionEnter2D(Collision2D collision) {
          if (collision.gameObject.tag == "Obstacle") {
              GetComponent<Rigidbody2D>().velocity = Vector3.zero;
              GetComponent<Rigidbody2D>().angularVelocity = 0;
          }    
      }
Comment
Add comment · Show 4
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 Shameness · Jul 04, 2018 at 06:31 AM 0
Share

If you mean pac-man stuck on intersections, possibly OnCollision being called continuously. You can debug whether its the case. Aside from this, pac man should take turn super precise to not stuck.

avatar image bilginba · Jul 04, 2018 at 08:16 AM 0
Share

First of all thank you for your response. I just started using the unity answers platform and it is really delightful to get answers to my problems. Firstly if i change continuously option most of the time player is rolling when it hits the corner of the object through the scene. If i use continuously player rolls rarely. Secondly to not stuck i should provide the environment and turning options from script. I understand like that but when i searched for the smooth movement for Pac-man i saw numerous methods people used. It might be my lack of knowledge about Unity but i couldn't use them as I want. So, I want to know can i handle my movement problem with rigidbody or should I try something like tilemaps (which I know a little bit about it).

avatar image Shameness bilginba · Jul 04, 2018 at 08:44 AM 1
Share

For both case tilemap would help a lot. For smooth movement you can use Time.deltaTime like this.

 if (Input.GetAxisRaw("Horizontal") > 0f || Input.GetAxisRaw("Horizontal") < 0f)
         {
             transform.position += new Vector3(h, 0) * speed * Time.deltaTime;
         }

However movement like above would not be precise, you would fine tune position while taking turn.

avatar image bilginba Shameness · Jul 04, 2018 at 10:09 AM 0
Share

Thank you for your help. I'll try my chance with tilemap.

2 Replies

  • Sort: 
avatar image
1

Answer by Vincent1236 · Jul 04, 2018 at 10:23 AM

If you want to use physics instead of coding the movement yourself, then use physics to stop your character as well. So I'd say remove the entire on CollisionEnter2D method.


Then your collision should stop you when running into walls. As for smooth sliding, add a circle collider to Mr. Pac Man, and put a Physics Material onto his collider that has friction set to 0 and set Friction Combine to Minimum

Comment
Add comment · Show 1 · 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 bilginba · Jul 05, 2018 at 12:22 AM 0
Share

Thank you for your answer but i don’t need it anymore. I redesigned my game according to tilebase

avatar image
1

Answer by hexagonius · Jul 04, 2018 at 09:28 AM

If you say moving like pacman, do you mean really moving like pacman? If so, you don't even need collision. Pacman moves on a grid with every tile tile on it known (floor, block, pickup...). You can just use simple math to move him and around corners.

Comment
Add comment · Show 1 · 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 bilginba · Jul 04, 2018 at 10:12 AM 0
Share

Thank you and yes I want movement like pacman but i was a little bit afraid of tilemap. It looked like kinda complex to me.However, I started some tutorials now and with grid it is easier to handle things as you said. It looks like more capable for a game like pacman.

Follow this Question

Answers Answers and Comments

101 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

Related Questions

Use transform.position in FixedUpdate to ensure collision with objects which have Velocity 0 Answers

How to activate all GameObjects named ,,GroundCollider'' by touching ,,GroundTriggerCollider'' and deactivating when no longer touching ''GroundCollider"? 1 Answer

Need a little help converting some line of JS to C# 1 Answer

How to disable circle rolling when it hits collision's corner? 0 Answers

Issue with object not bouncing around screen 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