Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Remembering where the player has been.

Discussion in 'Scripting' started by Kerrack, Sep 26, 2015.

  1. Kerrack

    Kerrack

    Joined:
    Sep 26, 2015
    Posts:
    3
    Hi,
    I'm quite new to unity and I have had an idea for a top-down 2d game in which the player can teleport around. However they can only teleport to places that they have been to previously. For the past few days I have been trying to work out how I could go about achieving this. Unfortunately I've been unable to find a way to track the players position and remember it as well as displaying this graphically when the player tries to teleport so they know where they can and can't go.
    If anybody has any ideas as to how I could do this then I would be very grateful.

    Thank you in advance to anybody who has any ideas.
     
  2. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    I can think of 2 ways:
    1: create a list of positions (List<Vector2>) and keep track of where the player has been in there (be sure to only add positions if they are not already in the list).
    2: create a class for each position on the ground (for instance, if you have a grid, add this script to each cell in the grid), and then use a boolean IsTeleportable or something to keep track.

    Is this enough to get you started? :)
     
    Kiwasi and Kerrack like this.
  3. Kerrack

    Kerrack

    Joined:
    Sep 26, 2015
    Posts:
    3
    Thanks for the quick response,

    I'd considered the second option before but I thought that would be quite intensive on the game if I had a grid of squares all with their own scripts running. Am I incorrect in thinking this?

    I'll be sure to try the first option anyway but how would you suggest displaying it graphically if I used this method?
     
  4. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    You could for instance add a collider (as a trigger) to each grid cell, and then when you add a position to the list - then you also do a physics2d.overlapcircle or something at that position.
    But there are many possibilities... if you have the time, experiment :)
     
    Kerrack likes this.
  5. Kerrack

    Kerrack

    Joined:
    Sep 26, 2015
    Posts:
    3
    Thanks for the help,
    I'll definitely spend some time playing around with your ideas.
     
  6. Rostam24

    Rostam24

    Joined:
    Mar 5, 2014
    Posts:
    119
    Also, if you have the time look at the rogue like tutorial on unity, I think that will definitely help out :)
     
    Kiwasi and Kerrack like this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is always enough time to look at a tutorial when you are stuck anyway.
     
    Rostam24 likes this.