- Home /
Problem is not reproducible or outdated
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;
}
}
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.
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).
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.
Thank you for your help. I'll try my chance with tilemap.
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
Thank you for your answer but i don’t need it anymore. I redesigned my game according to tilebase
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.
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.