I’ve in the process of remaking BurgerTime, as you can see I have quiet a bit done, but I’ve come to a mental block, and just can not figure out what the best approach is to move the character in this instance of this game.
Normally what I would do is a tile map but for this I changed my mind an did the artwork for the map in photoshop, as you would want to walk on the tiles in this instance.
AS you can see I created a series of markers this do dictate that the character can walk up/right/down/left, but what I’m finding hard is the best way to get it working in my mind.
For example, when this level starts the players starts at marker35, So he can go up/right and left, I thought about using the DOT product to get a marker that the character is looking at e.g
public MarkerComponent FindDirectlyinfront(Vector2 forwarddirection, Vector3 position)
{
Int32 mLength = SceneManager.Markers().childCount;
for (int i = 0; i < mLength; i++)
{
Vector3 mNormalized = (position - SceneManager.Markers().GetChild(i).position).normalized;
// Are we facing the next marker??
Single mDot = Vector3.Dot(mNormalized, position);
if (mDot >= 0 && mDot < 1)
{
MarkerComponent mTEmp = SceneManager.Markers().GetChild(i).GetComponent<MarkerComponent>();
//return SceneManager.Markers().GetChild(i).GetComponent<MarkerComponent>();
}
}
return null;
}
but this gives me marker33/34/35/36/37/38 if I’m wanting to go right.
I also thought of having circle collider 2D’s on the markers and them casting a ray from the direction we are looking, but I’m not to sure if again this is the right way.
Now I did just think of a new idea, Pixel perfect I could mask out (making a new bitmap) the areas that the character cannot travel in white then every time I want to move check this against the character if the pixel i hit is white(as in the mask), you cannot move?
Are any of these good idea’s or i’m I thinking this wrong?
hi there, i came here from that other thread…it is quite late and I would need to look that video again to understand how movement actually happens but If I understand you correctly, you want the person to move on a “photo”.
2 thoughts though…
1- Pixel collision could work (but I think it is not required, i can dig up a routine, I did this once in a fairly complicated setup)
2- colliders (I would possibly have 4 invisible colliders anchored to the center of the characters feet, 1 step in each 4 directions) - then all the walkable areas need a collider (box) and the person can only move in the direction, where one of his satellite colliders is colliding with a walkable area — this would require you to draw the box colliders, or you create them programmatically…
Yeah for sure, I also thought about edge 2d colliders as you can move them now easily by hand, I’m not sure if they are a single why though, the reason for this is that all enemies walk in from the edges into the map area, I know there is a collider that you can jump up though but not pass down it.
I did think of colliders around the map/surface/photo, but couldn’t think of a way for inbetween around the ladders “so he does not fall though”
I was looking though some old threads and you replied to me back in jan 17 about the car game, did you finish your version?
No, I did not finish the car game, abandoned it a while ago, but hopefully get back to it one day. It is basically ready but needs fine tuning of parameters and level design.
When I spoke of collides yesterday, I did not mean to use physics at all. Possibly, I should have used the word triggers. Effectively you draw a grid with collider that is walkable. Then you check in all 4 directions from current player position, if. Next move would still be on grid and only react to the joystick in case it is.
Enemies could use the same grid and if you want them you to walk in from the side have extra colliders that only work for them at the edges.
Hope you understand what I mean…could it work for u?
I might be in front of computer this evening and think about it better.
I would have created a class containing the direction the player can now go to. In the intersection of an horizontal path and a ladder going only in the top direction, this object contains " right, left, top". If the player is close enough from the intersection’s position vector2, he can choose these directions. If he goes top, he instantly looses “right” and “left”, but wins “down” (i believe if the player starts to climb he can choose to go back) and keeps “top”.
While he can go top / down, check the list of those markers to find those with the same x value in there vector2, y value if he can go right/left. (an almost equal value as it is a float, unless you can make sure there wont be float precision issues), this way you don’t have to check for every intersection in the scene, but only the ones you can walk across.
It requires creating a list of object containing an array of bool “directions” and one vector2 position for each intersection.
I did think of that very thing “It’s already there”, on each maker there are 4 directions nodes, this dictates as to which direction the player can walk in(and too the next node), yes I did think of using distance to check if they are near the intended next node, it was just if they turned around and went back that got me a bit puzzled, hence I thought of using the dot product, to say which node I’m facing? the code above a created gave me the wrong result, I think I should be using the position and the direction, not the two positions in this case.
I’m wondering, i don’t know how your monster IA is made, but if you need the monsters to know which node your player is actually going to so they can “bait him”, as your “direction” isn’t truly a rotation but more like four possible Vector2 (0,1 / 1,0 / 0,-1, / -1,0), if you know the two nodes composing your actual path, if you are going right, then the node you are aiming for is the node with a higher value on the x axis, and the one you are going away from is the one with the lower value.
I am not certain I explain what i mean well, if I find 15 min later I will type up the code, possibly easier than explaining. There are so many ways to do this, you just need to settle on the one you are most comfortable with. I will use either colliders and box check or a raycast for the player
I don’t like my proposed solution It is horrible to draw the map with colliders, they could be swapped out for sprites…you just need an easy system to get bounding boxes.
Anyway here it is…this is the script on the player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
public GameObject goMap;
public BoxCollider2D checkLeft;
public BoxCollider2D checkRight;
public BoxCollider2D checkUp;
public BoxCollider2D checkDown;
private BoxCollider2D[] map;
private float speed = 50f;
void Start ()
{
map = goMap.GetComponents<BoxCollider2D>();
}
void Update ()
{
Vector3 dir = new Vector3 (Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0f);
if (dir.x != 0)
{
if (dir.x < 0)
{
if (CheckOverlap(checkLeft, map) == false)
{
dir.x = 0;
}
}
else
{
if (CheckOverlap(checkRight, map) == false)
{
dir.x = 0;
}
}
}
if (dir.y != 0)
{
if (dir.y < 0)
{
if (CheckOverlap(checkDown, map) == false)
{
dir.y = 0;
}
}
else
{
if (CheckOverlap(checkUp, map) == false)
{
dir.y = 0;
}
}
}
transform.position += dir * speed * Time.deltaTime;
}
private bool CheckOverlap (BoxCollider2D dirCollider, BoxCollider2D[] mapCollider)
{
for (int i = 0; i < mapCollider.Length; i++)
{
if (dirCollider.bounds.Intersects(mapCollider[i].bounds))
{
return true;
}
}
return false;
}
}