0

I am trying to get characters to move around by randomly going left/right/forward at turns, and for the most part the characters do that, but every once in a while they walk through the walls, and I don't want that to happen. Is there anything you can see in my code that I am missing?

The walls have colliders on them, and so do the circles, but the circles colliders are just a trigger because I want the circles to be able to go through each other, just not through the walls.

It seems like the character turns, and some time turns again towards a wall.

Here is a video example: https://www.youtube.com/watch?v=ZOfGn3bsuLA

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(Rigidbody2D))]

public class Character : MonoBehaviour {

    public float speed;

    protected Transform frontRaycast, leftRaycast, rightRaycast;
    protected List<int> lastPossibleDirs = new List<int>();
    protected int lastDir;
    protected bool changed = false;
    protected bool forward, left, right;

    // Use this for initialization
    void Awake () {
        frontRaycast = transform.FindChild("FrontRaycast").transform;
        leftRaycast = transform.FindChild("LeftRaycast").transform;
        rightRaycast = transform.FindChild("RightRaycast").transform;
    }

    void Update(){
        // Test for a wall in front and on sides
        forward = Physics2D.Linecast(transform.position, frontRaycast.position, 1 << LayerMask.NameToLayer("Wall"));
        left = Physics2D.Linecast(transform.position, leftRaycast.position, 1 << LayerMask.NameToLayer("Wall"));
        right = Physics2D.Linecast(transform.position, rightRaycast.position, 1 << LayerMask.NameToLayer("Wall"));

        // Add each direction to the list of possible turns
        List<int> possibleDirs = new List<int>();
        if(!forward){
            possibleDirs.Add(0);
        }
        if(!left){
            possibleDirs.Add(1);
        }
        if(!right){
            possibleDirs.Add(2);
        }
        int dir = 0;
        if(possibleDirs.Count > 0){
            dir = (int)possibleDirs[Random.Range(0, possibleDirs.Count)];
        }

        if(changed){
            // Move forward with left or right option
            if(lastPossibleDirs.Exists(element => element == 0) && lastPossibleDirs.Exists(element => element > 0) && (left || right) && lastDir == 0){
                changed = false;
            }
            // Left
            else if(lastPossibleDirs.Exists(element => element == 1) && left && lastDir == 1){
                changed = false;
            }
            // Right
            else if(lastPossibleDirs.Exists(element => element == 2) && right && lastDir == 2){
                changed = false;
            }
        }else{
            switch(dir){
                case 0:
                    if(!left || !right){
                        transform.Rotate(new Vector3(0,0,0));
                        changed = true;
                    }
                    break;
                case 1:
                    transform.Rotate(new Vector3(0,0,90));
                    changed = true;
                    break;
                case 2:
                    transform.Rotate(new Vector3(0,0,-90));
                    changed = true;
                    break;
            }
            lastDir = dir;
            lastPossibleDirs = possibleDirs;
        }
        transform.Translate(Vector2.right * Time.deltaTime * speed);
    }

}

Here is a screenshot of where the detection areas are located on the character

Screen

1 Answer 1

1

Fist thing I would do is uncheck trigger on your circle and make use of

Physics2d.IgnoreLayerCollision(circle layer)

in the Awake()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.