穿过墙壁的人物

时间:2014-08-22 19:48:09

标签: c# unity3d

我试图通过随意左/右/向前转动来让角色四处走动,而且大部分角色都是这样做的,但每隔一段时间他们就会穿过墙壁,而我不会#39我希望这发生。你能在我的代码中看到我遗漏的任何内容吗?

墙壁上有碰撞器,圆圈也是如此,但是圆形碰撞器只是一个触发器,因为我希望圆圈能够穿过彼此,而不是通过墙壁。

似乎角色转过身来,有些时候又转向墙壁。

以下是视频示例: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);
    }

}

以下是检测区域位于角色

上的屏幕截图

Screen

1 个答案:

答案 0 :(得分:1)

我要做的就是取消勾选你的圈子并使用

Physics2d.IgnoreLayerCollision(circle layer)
Awake()

中的