- Home /
Restrict a player's movement to a path for a 2.5D game that has curved paths
There are some MMO games called "Fists of Fu" and "Elsword" that I was analyzing as I was trying to find recent examples of a 2.5d side scroller. There are points when the path "wraps around" a structure such as a mountain and although the path isn't straight, the character (and camera) rotates while making sure that the player still walks forward on that given. I'm assuming that only the player's X is being used for movement, but I don't know how I would make the character follow a path AND rotate accordingly.
This PvP recording shows the rotation I was going for: http://www.youtube.com/watch?v=LiK6UPtYCBw&feature=related and http://www.youtube.com/watch?v=yhrZRbT-wEk&feature=related
So how are they accomplishing this? I assume that the player and camera are being rotated on the Y axis, but how does the system determine when, (or how) to rotate the player and make sure that the camera is always facing the player's side?
BTW: I'm Using the 2D Platforming Tutorial as a base..
Edit: I Imported the spline controller for the Unitycommunity wiki: http://www.unifycommunity.com/wiki/index.php?title=Spline_Controller and I can have an object move around the path, but that's all it does. It doesn't rotate the object to make it face a constant direction (forward vector logic needed possibly?)
Answer by Loius · Oct 13, 2010 at 02:29 AM
I think the most effective and general purpose solution would be to use a Bezier Curve / 'Spline' interpolation technique -
Anything that interacts with the player would have something along the lines of:
var x : float; var y : float; private var spline : SplinePath;
function CalculatePosition() : Vector3 { return spline.WorldPosition( x ) + Vector3(0,y,0); }
A SplinePath would consist of a series of points, and when you ask it for a position it will choose a point along a smooth path based on those points.
The idea is that .WorldPosition(x) will calculate the ground level at a certain "X" position in the level, then you'll add on any vertical changes your character has (jumping, etc).
Obviously the tough part of this is the SplinePath.WorldPosition function, but there are quite a few examples of this on the web. If I remember correctly there's a very nice Spline script available on the unity3d forums in the scripting section. If it's not exactly what you need, it'll be pretty close. Try searching for Spline or Bezier curve paths.
Answer by pixelplacement · Oct 17, 2010 at 05:08 AM
Just put an example together on the support site for iTween that I hope helps out with this. If you parent the camera to the character in this example and offset it from its right shoulder you will be able to have a camera that follows around curves. Hope it helps: http://itween.pixelplacement.com/examples.php
Answer by Mattivc · Oct 13, 2010 at 08:14 AM
You should also take a look at iTween it has a few path interpolation options that migth help you out.
I've purchased an iTween package a couple of hours ago (only 2 buck) and I have half of what I need done! I just need to figure out the camera rotation part and I'm good!
Glad iTween was able to help out! Would love to see what you come up with!
I was going to post the project to see if anyone would be able to figure out why the rotation would work, but it includes some the the iTween PutOnPath code.. :(
Answer by Adam Rademacher · Oct 13, 2010 at 02:12 AM
You might have a trigger that tells your camera and player that they are on a corner. Then maybe a distance check and/or dot product once they enter the trigger to determine the orientation of the camera and player...A trigger based system is probably the easiest to implement.
I was thinking this, but I didn't know how to interpolate the orientation in code. I am trying to use iTween, but when I combine it with the 2d-platformer example, it's not working correctly...
Try using a Quaternion Slerp on the camera. It takes a from rotation, a to rotation, and a ratio (t). You set the from rotation to the original camera position, the to rotation to the sideways view, then base the ratio off of the distance travelled across the corner divided by total distance.
(Sorry for double comment) The other thing you could try is a Transform.RotateAround and set the angle of rotation based off of distance. You'd have a pivot point in the center of the corner, then rotate the camera
Thanks, I ended up using iTween to help me, but I still am having problems with it...
Answer by scone · Jan 07, 2013 at 03:25 AM
Hey there!
I'm the guy who created the spline system :)
I believe that it should do exactly what you're looking for. Oddly enough the code that I'm looking at doesn't have a flag that I remember adding for whether to affect the object's rotation. The default behavior should be to Lerp the object's rotation so that it's Z is looking down the spline in the direction that it's moving.
Lines 197-204 in SplineController should do that. If they aren't maybe your rotation damping value is at 0 for some reason? Let me know if you still can't get this to work!