Search Unity

Need help with scripting, got stuck on a problem where i cant really find the solution.

Discussion in 'Scripting' started by edvis1433, Aug 12, 2018.

  1. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    So im having this problem where im creating a pacman game just as a programming project for school and im kinda stuck on this one problem, where i want to make my pacman move from node to node and it doesnt it turns but doesnt move from its place, and when i delete the moving part from the code it moves, but not as i want, and that part what i delete is the code for moving from node to node, and somewhere in my code i get a null reference and i cant find it since im new to this, only have been coding in C++ so C# is diffrent for me and i havent been working on this kind of projects, so i would like someone to help me solve this problem.
     

    Attached Files:

  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Remember that
    Update()
    is the method that is going to get called every frame. That will be the entry point, per frame, for the game handling. At the moment,
    Update()
    only seems to be handling input detection and orientation setting.

    In terms of pseudo logic for
    Update()
    , maybe consider something along these lines:
    1. Is Pacman currently moving to a new node?
    2. If yes, then perform the appropriate movement for this frame.
    3. If no, has a key been pressed?
    4. If yes, then set the appropriate orientation, the appropriate movement values and flag that Pacman is currently moving.
    Note: this isn't perfect because, as given, it will probably give a one frame drop when travelling in a straight line through nodes. But hopefully it gives the overall idea. Does that help?

    Oh, also, please use Code Tags when pasting code. It makes it easier to read and refer to. :)
     
    edvis1433 likes this.
  3. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    Okay i get it what you are saying, but now im trying to figure it out how to make it work, i have very little experience in this and its kinda difficult for me, this whole thing that i have coded so far, took me long time to do, so i took a two month break from it and now everything is so confusing, but i know what you want me to do, so basicly i only update the orientation, but the problem is that im kinda sure that i dont need to use the update method for the movement, i use update for the button input and orientation, because i want my pacman move in a straight line until a button is pressed to change it, i dont want the update method to keep repeating the movement code every frame, i just want it to keep waiting for direction input. And im not sure how to make that work because im new to this and i probably have forgoten how my whole code is done, even tho i have been looking into it for some time, and i clearly need someone to keep helping me to figure some problems out :D
    If i wrote something really stupid im sorry, its just how i want my pacman to work, and i sorry if i didnt clearly understand your message.
     
    Last edited: Aug 12, 2018
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, I must admit as I think about it now I believe that is how the original controls worked as well. I seem to recall having to wiggle the joystick if I wanted to hold Pacman still waiting for an opportunity to sneak past a ghost.

    The problem here is that, one way or another, you have to update Pacman's position (almost) every frame. The possible options for this are: (1) the Physics engine, (2) InvokeRepeating, (3) a Coroutine or (4) the Update method.

    Given that Pacman's movement will play a role in virtually every frame, the Update method would seem to be the most appropriate location for that code.

    To give you an example of what I mean, try adding, say, a cube into your scene. Add the script below to it and try it out. Whilst it isn't quite right for a Pacman movement controller (the rotation isn't handled correctly, for example), you will notice that (a) movement is handled from within the Update method and (b) movement continues until a new direction key is pressed :-
    Code (CSharp):
    1. public class Test : MonoBehaviour
    2. {
    3.     void Update()
    4.     {
    5.         CheckControls();
    6.  
    7.         if (Vector3.zero != m_vely)
    8.             DoMovement();
    9.     }
    10.  
    11.     void CheckControls()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    14.         {
    15.             m_vely = Vector2.left;
    16.             transform.rotation = Quaternion.Euler(0f, 0f, 180f);
    17.         }
    18.         else if (Input.GetKeyDown(KeyCode.RightArrow))
    19.         {
    20.             m_vely = Vector2.right;
    21.             transform.rotation = Quaternion.Euler(0f, 0f, 0f);
    22.         }
    23.         else if (Input.GetKeyDown(KeyCode.UpArrow))
    24.         {
    25.             m_vely = Vector2.up;
    26.             transform.rotation = Quaternion.Euler(0f, 0f, 90f);
    27.         }
    28.         else if (Input.GetKeyDown(KeyCode.DownArrow))
    29.         {
    30.             m_vely = Vector2.down;
    31.             transform.rotation = Quaternion.Euler(0f, 0f, -90f);
    32.         }
    33.     }
    34.  
    35.     void DoMovement()
    36.     {
    37.         transform.position += m_vely * speed * Time.deltaTime;
    38.     }
    39.  
    40. #pragma warning disable 649
    41.     [SerializeField] [Range(0.5f, 3f)] float speed = 1f;
    42. #pragma warning restore 649
    43.  
    44.     Vector3 m_vely = Vector2.right;
    45. }
     
  5. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    Code (CSharp):
    1.     void Update () {
    2.  
    3.         CheckInput ();
    4.  
    5.         //Move();
    6.  
    7.         UpdateOrientation();
    8.     }
    9.  
    10.     void CheckInput()
    11.     {
    12.  
    13.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    14.         {
    15.             direction = Vector2.left;
    16.             moveToNode(direction);
    17.  
    18.         } else if (Input.GetKeyDown (KeyCode.RightArrow))
    19.         {
    20.             direction = Vector2.right;
    21.             moveToNode(direction);
    22.  
    23.         }
    24.         else if (Input.GetKeyDown (KeyCode.UpArrow))
    25.         {
    26.             direction = Vector2.up;
    27.             moveToNode(direction);
    28.  
    29.         }
    30.         else if (Input.GetKeyDown (KeyCode.DownArrow))
    31.         {
    32.             direction = Vector2.down;
    33.             moveToNode(direction);
    34.  
    35.         }
    36.     }
    37.  
    38.     void Move ()
    39.  
    40.     {
    41.  
    42.         transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime;
    43.  
    44.     }


    Isnt this like the same you gave me?
     
  6. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PacMan : MonoBehaviour {
    6.  
    7.  
    8.     public float speed = 4.0f;
    9.     private Vector2 direction = Vector2.zero;
    10.     private Node currentNode;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         Node node = GetNodeAtPosition (transform.localPosition);
    16.         if (node != null)
    17.         {
    18.             currentNode = node;
    19.             Debug.Log (currentNode);
    20.         }
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.  
    26.         CheckInput ();
    27.  
    28.         //Move();
    29.  
    30.         UpdateOrientation();
    31.     }
    32.  
    33.     void CheckInput()
    34.     {
    35.  
    36.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    37.         {
    38.             direction = Vector2.left;
    39.             MoveToNode (direction);
    40.  
    41.         } else if (Input.GetKeyDown (KeyCode.RightArrow))
    42.         {
    43.             direction = Vector2.right;
    44.             MoveToNode (direction);
    45.  
    46.         }
    47.         else if (Input.GetKeyDown (KeyCode.UpArrow))
    48.         {
    49.             direction = Vector2.up;
    50.             MoveToNode (direction);
    51.  
    52.         }
    53.         else if (Input.GetKeyDown (KeyCode.DownArrow))
    54.         {
    55.             direction = Vector2.down;
    56.             MoveToNode (direction);
    57.  
    58.         }
    59.     }
    60.  
    61.     void Move ()
    62.  
    63.     {
    64.  
    65.         transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime;
    66.  
    67.     }
    68.     void MoveToNode (Vector2 d)
    69.     {
    70.         Node moveToNode = CanMove(d);
    71.         if(moveToNode != null)
    72.         {
    73.             transform.localPosition = moveToNode.transform.position;
    74.             currentNode = moveToNode;
    75.         }
    76.     }
    77.  
    78.     void UpdateOrientation()
    79.     {
    80.         if(direction == Vector2.left)
    81.         {
    82.             transform.localScale = new Vector3 (-1,1,1) ;
    83.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    84.  
    85.         }else if (direction == Vector2.right)
    86.         {
    87.             transform.localScale = new Vector3 (1,1,1);
    88.             transform.localRotation = Quaternion.Euler(0, 0, 0);
    89.  
    90.         }
    91.         else if (direction == Vector2.up)
    92.         {
    93.             transform.localScale = new Vector3(1, 1, 1);
    94.             transform.localRotation = Quaternion.Euler(0, 0, 90);
    95.  
    96.         }else if (direction == Vector2.down)
    97.         {
    98.             transform.localScale = new Vector3(1, 1, 1);
    99.             transform.localRotation = Quaternion.Euler(0, 0, -90);
    100.  
    101.  
    102.         }
    103.  
    104.  
    105.     }
    106.  
    107.     Node CanMove (Vector2 d)
    108.     {
    109.         Node moveToNode = null;
    110.         for (int i=0; i < currentNode.neighbors.Length; i++)
    111.         {
    112.             if(currentNode.validDirections [i]==d)
    113.             {
    114.                 moveToNode = currentNode.neighbors[i];
    115.                 break;
    116.             }
    117.         }
    118.         return moveToNode;
    119.     }
    120.  
    121.  
    122.     Node GetNodeAtPosition (Vector2 pos)
    123.     {
    124.         GameObject tile = GameObject.Find ("Game").GetComponent<GameBoard>().board [(int)pos.x, (int)pos.y];
    125.         if (tile != null)
    126.         {
    127.             return tile.GetComponent<Node> ();
    128.         }
    129.         return null;
    130.     }
    131.  
    132. }
    133.  

    This is my whole code for the pacman itself to move and stuff, and i get null referance somewhere, im not sure why, i think i just need that to be fixed and i should be fine but i dont know how.
     

    Attached Files:

  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I can't see where you are moving Pacman - but possibly I am simply missing something. The point of the code I posted was that a single keypress will cause continuous movement in one direction until the next valid key press that changes to another direction.

    Is your code moving Pacman then? I understood from your original post that Pacman wasn't moving - "i want to make my pacman move from node to node and it doesnt it turns but doesnt move from its place".
     
  8. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    Before the node to node movement i have done that, but then pacman could move anywhere, and had no limits, and the movement from node to node keeps him in place not to go through walls and stuff
     
  9. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, I see what you are getting at. But how are you actually moving Pacman in the node-to-node code above? Am I simply missing it?

    So to try and clarify where you are right now: (1) is Pacman currently moving or not? (2) If yes, is Pacman currently able to go through walls or not?
     
  10. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    Pacman cant really move right now, because there is a problem with the node-to-node movement code and if i delete it then my pacman can move go through walls.
     
  11. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    And currently im kinda lost with my code, not sure what is for what, but what i know that (Vector2 d) is a null for some reason, atleast i think so because in the console there are like 4 errors with (Vector2 d)
     
  12. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, so it looks like you want to be clear on how you want to approach this problem. There will be a number of ways of achieving this (physics engine, navmesh, 3rd party character controller, your own handlers).

    So the first question is, are you wanting to code this all yourself (i.e. your own movement handling) to get a feel for coding, or are you happy to use other systems/ API's (physics engine, navmesh, character controller) to help you?
     
  13. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    im fine with not coding all by myself, because im short on time with this project, i need to get it done until next month, or atleast have it working that i could start it and play it.
     
  14. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Ok, so I don't know what your budget is like. For the Navmesh option, there is Nav2D.

    Now, whilst looking around for an appropriate character controller, I came across this physics based tutorial for making a Pacman game. Would that be helpful to you at all?
     
  15. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    So basicly i didnt even need to put the code to move from node to node, i could have made a collision detect right?
     
  16. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Either method will work. But if you are finding directly handling movement tricky, then sure, give the RigidBody / collision method a try. See how you get on. :)
     
  17. edvis1433

    edvis1433

    Joined:
    Apr 11, 2018
    Posts:
    19
    Yeah i think the directly handling movement from point to point is kinda tricky for me to get it working properly and still need to learn a lot, so i guess the collision method is better for me atleast for now, thanks for all the help
     
    Doug_B likes this.