I’m working on a movement system where the camera is constrained to a rail/spline while the player is able to move within the bounds of the camera view. However I’m having issues with getting the player to move relative to the camera view.
The diagram below shows my current setup.
At the moment I have everything setup so that the camera is attached via a springarm to a ‘dummy’ mesh that moves along the rail and the player (red cube) is a child of the camera that can move in the Y and Z axes within the bounds of a defined area of the camera view. However when the camera follows a curve round and in the direction of the X axes, the vertical movement of the player remains fine but the the horizontal movement remains locked to the world Y axes instead of rotating with the camera.
So I am looking for help in modifying my player movement to keep it relative to the camera. Basically what I’m looking to achieve is having the player move on a plane relative to the camera, represented in the diagrams below by the light blue plane, so that as the camera follows the curve of the rail/spline (A -> B) the player’s movement axes remain aligned to the camera rather than the world.
Does anyone have any suggestions as to how I could achieve this?
My current player movement is setup as shown below.
Ok, I’m not completly sure of what I’m about to say, but since you have no other answer, I’ll take the risk to make a suggestion.
From what I undertood, you need to make a player movement very close to what can be found in third person or first person template. You just need to replace the use of forward and right vectors with something new, that should comes out from e a mix of the camera view and the character pawn (since it’s still the entity interacting with the world in the end).
I’m not completly sure, but I think you should somehow project the camera axis on a plane. And this plane should be horizontal to the player. By projecting the Y and Z axis of your camera on this plane, you should obtain the new right and forward vectors of your player (If I remeber orientation correctly). From here, you can just execute movement normaly.
Does that seem helpful ?
Hi killfassil,
Thanks for the reply. Your suggestion is pretty much the conclusion I was coming to. I now just need to figure out a way to project the camera axes onto a plane.
Set your character movement component to snap the character’s movement to a plane.
Get the Camera’s forward vector, and every Tick feed that value to a “Set Plane Constraint Normal”. This will force the player to only move relative to the camera’s forward position.
The only issue here is if the camera is pointed at an angle that vector will be incorrect, since it won’t be properly NORMAL to the plane you want the player on (i.e. if the camera is pointed down slightly at the player rather than directly side-on). And you do invite some issues there as well with the camera’s rotation interpolating strangely and the player slowly moving off-track from what you desire.
For my game (which is 3D with 2.5D segments) I use boxes which snap the player to their plane origin and normal on contact. To do “curved 2D” as you’re after, I have a blueprint with a spline component that spawns them at specified intervals along itself. It can take hundreds of trigger volumes to get a smooth feel but the performance hit is basically nil. I then let the camera work more freely, simply setting a control rotation that matches the view I want, which allows me some leeway with the camera movement (via lerps) while still keeping the player firmly attached to the correct linear path without the possibility of him veering from it if the camera turns slower than he changes zones.
Hi RhythmScript,
Thanks for the ideas. I’ll give them a go and see where it takes me.
Just wanted to come back and say thanks again to RhythmScript for the idea of using boxes aligned along a spline to guide the player round curves. I’ve managed to implement this system into my character movement and it works surprisingly well and is far neater than my approach of locking to a spline path or using traces etc. to detect splines.
My only issue now is that if my character starts at 0 on the X axis with the guiding path also aligned to start at 0 on the X axis, and then moves along my curved path, if I go back along the path and return to the starting location my character doesn’t return to 0 on the X axis and retains a slight offset.
Does anyone have any suggestions as to how I can eliminate this offset?