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. Dismiss Notice

How to check if a player follows a specific path and save results

Discussion in 'Scripting' started by unity_SbcQf9pJ4b6pkA, Apr 4, 2022.

  1. unity_SbcQf9pJ4b6pkA

    unity_SbcQf9pJ4b6pkA

    Joined:
    Feb 7, 2022
    Posts:
    12
    Hello everyone,

    I'm new on Unity and Unity forum so I hope my post will be in the right section and appropriate.

    To begin with, some context : I'm trying to develop a serious game (here a 2D platformer) which aims to generate data about player's ability to go through obstacles like : how many time for level X, number of jumps, number of keys pressed etc.. Some basic numbers to begin with.

    Now I'm trying to develop a new feature that's more complex. I want to ask the player to follow a line while flying (by holding a key pressed) and to evaluate his process like : if his movements aren't precise his score won't be so good, reverse if he's precise, if he has tendency to stay over the line or under ? How many time did he went from over to under the line ?

    Do you understand what I mean ? Here's a schema : He's ok, he stays relatively close the line.png

    Do you got any idea on how to proceed ? Which functions use ? Or anything that can help me on this ?

    Hope I've been clear enough, my English isn't that good.

    If you need any informations more, I'll be happy to give it to you.
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    The first idea that comes to mind is measuring the players distance to the line.

    Your illustration shows a straight line but I gather that might not exactly be accurate, but maybe it is? If it is then great, your job is nice and easy, if not its a bit more meh.

    You essentially need to find the point on the line that corresponds to the players current position.

    Imagine something like this:


    You need to find the point on the line that corresponds to the players current x position.

    Once you have that you can run distance calculations on the player compared to the line.
     
  3. unity_SbcQf9pJ4b6pkA

    unity_SbcQf9pJ4b6pkA

    Joined:
    Feb 7, 2022
    Posts:
    12
    I only need some kind of MVP project in order to present in front of a jury so I'll do it with a straight line it will be much more easier. Maybe something like in update method of playermovements script compare make something like :

    Code (CSharp):
    1. score += player.positionX - line.positionX
    And this should be put in absolute value I think In order to have a positive score and the bigger it gets the worst the movement was.
    Does it sounds good to you ?

    Do you know how can I get line.positionX at any time ? I'm not sure it's the correct syntax

    Thank you
     
  4. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    It depends.

    If we assume that this is your player:

    Where the y value is the distance from the line, then you want the score to be based on the y value.

    Given that your line is straight you can assume that the x value for the line will be the same as for your player.

    The y value can be calculated as a displacement from y=0, and do your score math accordingly.

    Honestly, you really only care about the y value displacement, nothing else is important at this stage.
     
  5. unity_SbcQf9pJ4b6pkA

    unity_SbcQf9pJ4b6pkA

    Joined:
    Feb 7, 2022
    Posts:
    12
    Yeah quite right I confused myself between X and Y I wanted to write positionY I guess.

    Gonna try, thanks !
     
  6. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Linerenderer seems like it would be perfect for this, it's just a matter of extracting the data from the rendered line in co-ordinates, what you could do is create vertices and store them along the line path that is being drawn by the player and then add those vertices to a list and keep track of everything that way. That's the method I would consider with this.

    https://docs.unity3d.com/ScriptReference/LineRenderer.html

    Since you're clearly wanting to do a serious speed running style platformer, I would recommend to keep it as realistic as possible just running through the game yourself and then using the linerenderer vertices that way. Titanfall for example did something similar to this with the speed runs when it came to the parkour sections in the training missions. If you just pre-defined some paths a lot of players might rightly think this is just dev cheating to get a result that they can't beat.

    So here's what I would do, create a linerenderer, do a 'dev' speed run of the game, save the generated vertices from the linerenderer to a list. You would then do another speed run which would generate another list of vertices and then do a foreach loop to distance check each of the vertices from both lists to compare how you did.