Am working on a 2.5D game right now and am having trouble finding a method that I can use in this situation I want the player to be able to move backward but I don’t want them to be able to go all the way to the start of the level I want to control how far back the player can travel before they are stopped like in super Mario bros. you can go forward but only travel back so far. I was thinking about the (Mathf.clamp) method to clamp the x axis but not really sure if that will work or how to go about it. look forward to any help Thanks.
i guess could try something like,
float furthestDistanceTraveledX = 0;
float allowedDistanceBack = 50;
// keep this value as largest X coordinate the player has visited
furthestDistanceTraveledX= Mathf.Max(furthestDistanceTraveledX, player.transform.x);
// then don't allow playerX to go below furthestDistanceTraveledX-allowedDistanceBack
if (player.transform.x<furthestDistanceTraveledX-allowedDistanceBack) ... then don't allow moving backwards
Thanks for the speedy reply ill try this out as soon as I can and post how it go’s but in makes sense. I thought about having an empty GameObject with a box collider and attaching that to the player as a child so it will be a following barrier. the problem is that because it is a child when the player go’s backward the collider will just follow defeating the purpose. but now as am writing this maybe I can have the empty GameObject unparent when the player go’s backwards and parent back when player moves forward. Thoughts??
yeah that could work also, or just move the box collider into that (furthestDistanceTraveledX-allowedDistanceBack)
I was also thinking about making a timer to set the x pos. only when the left arrow key or (A) key is held down something like this
private Vector3 CurrentPos;
Private Int Timmer;
void update()
{
if(input.getkey(keycode.LeftArrowKey))
{
Timmer - = 1 * time.deltatime
if (Timmer <= 0)
{
transform.postion = new vector3(CurrentPos, transform.postion.y, transform.postion.z);
}
}
}
Am not sure if it will work pending testing.
It half worked. if the Player presses the button you can move back until time reaches 0 then you stop. when you lift the button the Timmer resets to 3 sec and you can move back another 3 sec. so if works and doesn’t work.
I would use the barrier idea, but not have it as a child of the player. I would make a reference to the player so I can evaluate it’s position each frame.
When the player moves to the right, the barrier follows at a certain distance. When the player moves to the left, the barrier simply stays in place.
To keep it simple and clean I would just have the barrier store the player’s position in a Vector3. In the Update method, this Vector3 is only overwritten when the player’s X position is higher then the one already in the vector. That way this Vector3’s X value can only increase, never decrease.