- Home /
How to move like Pacman/Snake using Raycasts inside a 2D array ?
I have a grid based level with each grid sized 1x1 unit(default). How do I make my gameobject move through the lanes accelerating in a specific direction (Pacman) until an input is given to change its' direction, or if it collides with a deadend wall and turns to its' right ? The movement needs to be aligned to the middle of the tile. An example of the level with the player object is given below. The green object is the player and red cylinder is a goal, and the scenario in the picture is when no input button was pressed, hereby allowing the player object to auto-turn left.
Raycast forward, then if distance to object if less than 1 or whatever, change direction.
Answer by Kiwasi · Oct 01, 2014 at 06:23 PM
Pacman movement is done using a 2D array of possible positions. I would build an array for each level, then use that array for collision detection.
I had a working version of a script that does exactly that. However, the movement was happening only when a direction key was pressed, making it a manual movement, ins$$anonymous$$d of automatic movement. Also, getting the auto turn to work is making me go nuts. Here is that script. $$anonymous$$indly note that a grid inside the array with a value of 1 is the unwalkable tile.
public GameObject levelCreator_;
levelCreator temp;
Vector3 playerPos;
int[,] mapArray = new int[13,17];
public bool inhibitPlayerInput;
void Start () {
DOTween.Init(true, true, LogBehaviour.Verbose).SetCapacity(6000, 6000);
levelCreator_ = GameObject.Find ("LevelCreatorGameObject");
temp = levelCreator_.gameObject.GetComponent<levelCreator>();
mapArray = temp.mapArray;
for(int i = 1; i<12; i++)
{
for(int ii = 1; ii < 16; ii++)
{
if( mapArray[i,ii] == 2) // tile with value 2 is the starting position of the player
{
playerPos = new Vector3(i,ii,0);
}
}
}
transform.position = new Vector3(playerPos.x,playerPos.y,0);
}
void Update () {
if (inhibitPlayerInput) return;
getInput();
}
void getInput()
{
bool inputPressed = false;
Vector3 newPlayerPos = playerPos;
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.W))
{
inputPressed = true;
newPlayerPos += new Vector3(-1,0,0);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.S))
{
inputPressed = true;
newPlayerPos += new Vector3(1,0,0);
}
else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
{
inputPressed = true;
newPlayerPos += new Vector3(0,-1,0);
}
else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.D))
{
inputPressed = true;
newPlayerPos += new Vector3(0,1,0);
}
if (!inputPressed) return;
if (mapArray[(int)newPlayerPos.x,(int)newPlayerPos.y] == 1) // Unwalkable tile check
{
return;
}
playerPos = newPlayerPos;
if(mapArray[(int)playerPos.x,(int)playerPos.y] == 0 || mapArray[(int)playerPos.x,(int)playerPos.y] >= 2)
{
inhibitPlayerInput = true;
transform.DO$$anonymous$$ove(playerPos, TweenWpDuration).OnComplete(() => inhibitPlayerInput = false).SetEase(Ease.Linear);
return;
}
}
I'm using a RigidBody2D and velocity ins$$anonymous$$d of position. To make it easier on myself, I set up velocity vectors named "up", "down", "left", "right", and "stop".
snabbott , can you share your code for pacman moving, please.
Your answer
Follow this Question
Related Questions
Physics raycast hit offset from where it should be 1 Answer
Raycast to ignore caster (Not by layer) 1 Answer
My raycast does not work as espected 2 Answers
Add collision to raycasted object? 2 Answers