Search Unity

Unity PacMan

Discussion in 'Scripting' started by computerg767, Jan 31, 2015.

  1. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    so I am copying this exact tutorial on how to code pacman

    http://noobtuts.com/.../2d-pacman-game

    currently on

    The Movement Script
    and type the code as it shows on the website.

    the code seems to work but when ever i use the arrow keys it doesnt want to do anything.

    Heres my code:



    using UnityEngine;
    using System.Collections;

    public class PacmanMove : MonoBehaviour {
    public float speed = 0.4f;
    Vector2 dest = Vector2.zero;

    void Start() {
    dest = transform.position;
    }

    void FixedUpdate() {
    // Move closer to Destination
    Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
    rigidbody2D.MovePosition(p);

    // Check for Input if not moving
    if ((Vector2)transform.position == dest) {
    if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
    dest = (Vector2)transform.position + Vector2.up;
    if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
    dest = (Vector2)transform.position + Vector2.right;
    if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
    dest = (Vector2)transform.position - Vector2.up;
    if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
    dest = (Vector2)transform.position - Vector2.right;
    }
    }

    bool valid(Vector2 dir) {
    // Cast Line from 'next to Pac-Man' to 'Pac-Man'
    Vector2 pos = transform.position;
    RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
    return (hit.collider == collider2D);
    }
    }
    If you could help me that would great!
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Can you write the values to the log to see if they are as you expect (e.g. KeyCode.UpArrow, Vector2.up and the result of valid(Vector2.up)?
     
  3. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    I am kinda new to coding so how would you go about doing that?
     
  4. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
  5. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    I did exactly what it told me to do in the tutorial.
    which told me to add a script directly to pacman
     
  6. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    Kind of hard to tell without seeing the project... All I can do is guess what may be the issue...

    Did you place PacMan so that its collider overlaps a wall, by any chance? Did you remember to set the radius of its circle collider to 0.95?
     
  7. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    yep did all that
    the collider does not over lap the wall they told be to put pacman in a certain spot which I did do
     
  8. Oana

    Oana

    Joined:
    Sep 28, 2012
    Posts:
    87
    Not sure what else to suggest then. Maybe you can post the project?
     
  9. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    how do i go about doing that?
     
  10. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    heres the file
     

    Attached Files:

  11. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    There are two things that could be going wrong - either your movement code is not working, or your input code is not working. How do we find out which it is? The first step is to determine what the value of "dest" is. We can do this easily by selecting Pac-Man and setting the inspector to Debug mode.

    Like so: https://www.dropbox.com/s/b0ghkt8q7pkngnb/Screenshot 2015-02-01 13.27.05.png?dl=0

    Now, when you run the game, look at the values of "Dest" as well as the Pac-Man transform.

    Before running: https://www.dropbox.com/s/jrozbc73rubmbxc/Screenshot 2015-02-01 13.27.54.png?dl=0
    After pressing play: https://www.dropbox.com/s/n9mla32rzqlqh95/Screenshot 2015-02-01 13.28.11.png?dl=0

    "Dest" appears to be set properly, but it does not change when you press an arrow key, which means that your input code is for some reason not changing dest. You can confirm that it's not being called by adding this line:
    https://www.dropbox.com/s/m1pok6hcfk0mdnn/Screenshot 2015-02-01 13.29.00.png?dl=0

    If your project is like mine, "Checking Input" is only logged once, which means that the input check is failing after the first frame. How do we solve this?

    Code (csharp):
    1.  
    2. if (transform.position == dest)
    3. {
    4.  //...
    5. }
    For some reason, transform.position is not equal to dest, even before you've moved. That can only mean that for some reason, the engine is moving pac-man around and thus the input code won't run.
    Look again at the "after pressing play" screenshot: https://www.dropbox.com/s/n9mla32rzqlqh95/Screenshot 2015-02-01 13.28.11.png?dl=0

    See how Pac-Man is no longer at 14,14, but 14, 13.9…? If you focus on Pac-Man when you press play, you can see that he moves a bit. If you look at the collider map, you can see that your collider placement for the background is slightly off in this area, and so that collider is pushing Pac-Man a bit off-kilter, and breaking your code.

    Ok, how do you fix it?
    1. Easy solution: shrink Pac-Man's collider a bit to compensate for errors in the level layout.
    2. Harder solution: go through one-by-one and line up the level colliders properly.

    Overall, finish the tutorial if you want to, as it'll likely be valuable learning experience either way, but I'd be wary of starting another one off of this website as the methodology is a little iffy (using rigidbody physics for a pac-man clone ain't the best idea I've ever heard).
     
    K1327399 likes this.
  12. computerg767

    computerg767

    Joined:
    Jan 30, 2015
    Posts:
    7
    I fixed it was the circle collider was in the way
     
  13. Dano13

    Dano13

    Joined:
    Jun 29, 2015
    Posts:
    4
    Are you sure that changing the circle collider fixes the problem?

    I have been smashing my head against a wall for a couple of days with this issue and am having problems with the pacman sticking against walls.

    Note: The tutorial was done in Unity 5 and I am working in Unity 4.6, but I can't believe that such basic features would be changed in the new version.

    Anyway - checking out the tutorial ... http://noobtuts.com/unity/2d-pacman-game

    Everything is fine until you try and script the pacman movement. After analysis I believe the problem is a logic problem in the script. At first I thought it was colliders penetrating one another, but that isn't it.

    In the script - see how you have

    // Check for Input if not moving
    if ((Vector2)transform.position == dest)

    I believe this conditional is the root of the problem. It is only checking input when the position of the pacman is the same as the destination which is usually 1 unit away on key presses.

    However, suppose a wall collider is slightly more than 1 unit away. Let's say it is 1.4 units from Pacman. Lets say that the X coordinate of Pacman is 19, and the collider of the near wall that it is moving towards is 20.4.

    Now as Pacman is moving along, the next value (dest) is at X 20. Using the linecast 20 is cast towards 19 and no colliders are hit, so 20 is a valid place according to the script.

    So pacman moves towards 20. However the collider has a radius of 0.95, so as it tries to move to 20, the edge of the circle collider collides with the wall at 20.4. This gets bumped back to say 19.5.

    With me? But Dest isn't updated. It stays at value 20, so the script for pacman keeps forever trying to get to that point, bumping against the collider, and ... here is the important point ... it never reads the section pertaining to reading input values, so pacman appears stuck, even though what is really happening is he is ignoring all keyboard input.

    Not sure the best way to address this logic flaw - however I did find another pacman script that was based on this tutorial, even though it doesn't have the same logic flaw.

    Of course I am fairly new to Unity so my reasoning could be completely off here - but these are my conclusions.

    https://github.com/vilbeyli/Pacman
     
    K1327399 likes this.
  14. tljconan

    tljconan

    Joined:
    Feb 22, 2016
    Posts:
    1
    I'm Chinese,So I'm sorry that My English is not good.
    Run the game and the Pacman don't move.
    Its "transform.position" is (14,14) changed (14,13.996).
    I found that this bug is becaues of Pacman's component-"Rigidbody 2D".
    Because "Rigidbody 2D" have "Mass", so Pacman will be dropped a little, so (14,14) changed (14,13.996).
    My solution is to Pacman's "Rigidbody 2D"-->"Is Kinematic"-->"NIKE[√]"
     
    K1327399 and gdossant like this.
  15. shoaibmughal

    shoaibmughal

    Joined:
    Dec 27, 2016
    Posts:
    1
    hello Bro how you resolve this Error because i am facing same error because i am seen same tutorial like you but i was totally stuck with this error how can i resolve this my pac man no move to any other side please tell me
     
  16. muchtarps

    muchtarps

    Joined:
    Aug 10, 2015
    Posts:
    1
    It is just because the recent unity doesn't use 'fixed angle' again. You need to check freeze rotation on Rigidbody 2D and root motion on Animator.
     
  17. gdossant

    gdossant

    Joined:
    Sep 24, 2017
    Posts:
    1
    this worked, thanks man
     
  18. AltairaJade

    AltairaJade

    Joined:
    Sep 22, 2018
    Posts:
    2
    Changing the collider size did not address the issue. Exactly what did you do?
     
  19. AltairaJade

    AltairaJade

    Joined:
    Sep 22, 2018
    Posts:
    2
     
  20. aliaanadzirahh00

    aliaanadzirahh00

    Joined:
    Jul 6, 2022
    Posts:
    1
    incase someone needs this, just change 0.95 to 0.9 at circle collider 2D of pacman
     
  21. nadiraa

    nadiraa

    Joined:
    Mar 31, 2021
    Posts:
    2
    I followed your instruction and changed the collider 2D Radius to 0.9. The pacman still is not moving when the arrow keys are pressed.
     
  22. nadiraa

    nadiraa

    Joined:
    Mar 31, 2021
    Posts:
    2
    Very confused. I changed the Rigidbody 2D Body Type to Kinematic. The pacman is still not moving.
     
  23. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689

    Please don't necro-post. If you have a new question, make a new post. It's FREE!!




    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?



    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - ONLY post the relevant code, and then refer to it in your discussion.




    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.