Search Unity

Problem follow path while tracking player x position

Discussion in '2D' started by Rawlin, Apr 25, 2017.

  1. Rawlin

    Rawlin

    Joined:
    Apr 1, 2015
    Posts:
    7
    Hello,
    Been googling with no luck.

    Im trying to make an object track the players x position while following a path made of emptys. So it maintain the same x position as the player but on its path. This is so I can setup a pre-defined camera path.

    Could use a pointer in the right direction. Anyone know of a tool, thread or tutorial? Im using 5.6. Edit: Forgot to write Im using C#.
     
    Last edited: Apr 25, 2017
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    What you want is called a Spline. There are some spline assets available on the asset store. I use Super Splines. I don't know if it's the best spline asset out there but it does the job for me.

    What I do for me camera is use a function available in Super Splines that gets the closest point on the spline (path) to the player character. This isn't exactly what you're asking for but it does a pretty good job.
     
    Rawlin likes this.
  3. Rawlin

    Rawlin

    Joined:
    Apr 1, 2015
    Posts:
    7
    Thank you :) I'll check that out!
     
  4. Rawlin

    Rawlin

    Joined:
    Apr 1, 2015
    Posts:
    7
    Don't think that's what I am looking for unfortunately. This is what I am trying to do. Don't need to be splines. Even though it would be cool with these soft curves. While doing the illustration I think I can see a flaw in my method... It needs to follow player but locked on it's path.

    http://imgur.com/9cOhC6S
     
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Splines don't have to be curves, they can be straight lines as well if you like. All a spline will let you do is easily determine where along a line you are, what the closest point is on a line etc. These methods are all useful in doing whatever it is you are trying to achieve. If you don't use a spline like the one i mentioned above then you'll probably end up coding something similar anyway, to figure out which two nodes you should be between and what percentage between those nodes you are..

    From the looks of your diagram you'll need the camera to be a little smarter than following the player's x position because otherwise the player will fall off the bottom of the screen and the camera will be looking inside the wall on the right. You want the camera to follow a path (a spline) closest to the player position and then offset the camera from that path by a unit or two to the right.
     
  6. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    Or you could use triggers to set a new camera offset form the player's position as the player moves through the level. you'll need to blend that offset overtime but you could get some decent results doing that. I do that in my game Major Mayhem, you can see the results in our trailer video here

    http://www.landingpad.rocketjumpgames.com/majormayhem

    You'll find there are a bunch of edge cases and exceptions that you'll need to program in as well. Like for example if your character can jump, then you will not want the camera to follow the player's y position as they jump because that looks bad, but if they player falls then you do because you don't want the player to leave the bottom of the screen, unless they are falling to their death then maybe you do want them to fall off the screen... you see there's a bunch of things and you'll need to just discover them as you go.

    But ultimately if your levels are very constrained (a single path) then getting the camera to follow a spline will give you the best and most predictable results, with the most flexibility in terms of nice camera motion and character framing in different situations.
     
  7. Rawlin

    Rawlin

    Joined:
    Apr 1, 2015
    Posts:
    7
    Thank you for the input :)
    The problem I got is not getting the camera to follow a path or spline, but to follow the player while being locked to it. If the player stops or moves backwards I get some strange results. And if I set the camera to always move it will move away from the player.

    A little bit hard to describe, but it's a sidescroller and the player flies most of the time, and keeps a steady speed unless he get a speed boost or stop. Problem comes when the level is not a straight line.

    Falling off the screen should cause the player to die, unless it's meant to go down at that point. That's what I figured a track would be the best option for.
    As an example in the sketch the path goes down on that little fall at the end and the camera should follow a long the path. Even if I would have to make two dots to make the camera start moving down a little bit earlier to even it out. It would be preferred with a spline but a straight path would work fine for me too. As long as it maintains the same x position on the path as the player has on the level.

    Ive tried bounding boxes, while it works it's kind of not what I would ultimately want. It can get hard when switching boxes. I've also looked into just changing the cameras position between points if it reaches a trigger or using a lane system. Problem with the lane system is that I do not want the player to be able to fly to high, only switch screens at certain places, and I wouldn't be able to make the camera travel up or downhill at an angle. But I could live with that.

    I did look up Super Splines, and I have tried to find one that can track a target while being locked to a path, but so far I have not been able to figure out if they can or not, and I can't spend money just to try it out.
     
  8. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    I use super splines in the game I'm developing at the moment to do exactly what you're saying.

    I use the function on a spline in super splines that gets the closest point on the spline to a given point and I pass in the character position in the world. The spline will then calculate the closest point on the path to the character. I then set the camera to that point. It works no matter where or how the character moves. If the character teleports then the camera will to, if the character moves left the camera will to.

    Like I said, I don't know if super splines is the best/cheapest solution. All I know is it does what you need it to because I am using it for exactly that purpose.

    Here's the documentation that demonstrates how to do it.

    http://www.evoluo-technologies.com/data/evoluo/docs/supersplines/example2.html
     
  9. Rawlin

    Rawlin

    Joined:
    Apr 1, 2015
    Posts:
    7
    Thank you, that's what I needed to know, that it could do it :)
    Doesn't need to be the cheapest option, just need something that works. Think super splines might be it then. I'm currently trying out lanes, and I kind of like how it works, going to try it out for a bit before I go for the splines.

    Thank you again :)