Beispiel #1
0
    public override int CanDoAction(AAnimal myself)
    {
        RaycastHit hitFront; RaycastHit hitDown;
        Ray rayDown = new Ray(myself.nextnextPOS + new Vector3(0, myself.ObjectScale.y - 0.5f, 0), Vector3.down);

        if (Physics.Raycast(rayDown, out hitDown, 2 * (myself.ObjectScale.y - 0.5f)))
        {
            if (hitDown.collider.gameObject.layer == LayerMask.NameToLayer("Environment") ||
                hitDown.collider.gameObject.layer == LayerMask.NameToLayer("Animal"))
            {
                myself.nextnextPOS += new Vector3(0, myself.ObjectScale.y - 0.5f - hitDown.distance, 0);
            }
            else if (hitDown.collider.gameObject.layer == LayerMask.NameToLayer("Terrain"))
            {
                myself.nextnextPOS += new Vector3(0, Terrain.activeTerrain.SampleHeight(myself.nextnextPOS) - myself.nextnextPOS.y, 0);
            }
        }
        else
        {
            return (int)ErrorTypeList.Move;
        }

        Vector3 dir = new Vector3(myself.nextnextPOS.x - myself.nextPOS.x, myself.nextnextPOS.y - myself.nextPOS.y, myself.nextnextPOS.z - myself.nextPOS.z);
        dir = myself.RoundToIntVector3XZ(dir);
        float maxd = 1.0f;
        if (dir.x != 0 && dir.y != 0 && dir.z != 0) { maxd = 2.25f; }
        else if (dir.x != 0 && dir.y != 0) { maxd = 1.5f; }
        else if (dir.y != 0 && dir.z != 0) { maxd = 1.5f; }
        else if (dir.z != 0 && dir.x != 0) { maxd = 1.5f; }

        Ray rayFront = new Ray(myself.nextPOS + new Vector3(0, myself.ObjectScale.y - 0.5f, 0), dir);
        if (Physics.Raycast(rayFront, out hitFront, maxd))
        {
            if (hitFront.collider.gameObject.layer == LayerMask.NameToLayer("Animal"))
            {
                if (myself.name == hitFront.collider.gameObject.name) { }
                else
                {
                    return (int)ErrorTypeList.Move;
                }
            }
            else if (hitFront.collider.gameObject.layer == LayerMask.NameToLayer("Terrain") ||
                hitFront.collider.gameObject.layer == LayerMask.NameToLayer("Environment"))
            {
                return (int)ErrorTypeList.Move;
            }
        }
        return (int)ErrorTypeList.Nothing;
    }
Beispiel #2
0
    public override int CanDoAction(AAnimal myself)
    {
        jumpRouteList.Clear();
        nextpos = myself.nextPOS;
        jumpRouteList.Add(nextpos);
        currentRun = myself.CurrentRun;
        currentJump = myself.CurrentJump;
        bool landed = false;
        Vector3 endpos = myself.nextPOS;
        bool clashed = false;
        Vector3 dir = myself.DIR;
        float jumpsum = 0.0f;
        for (int i = 1; i <= currentJump; i++) { jumpsum += i; }
        RaycastHit hit;
        Ray ray;
        float distance = 1.0f;

        float sum = 0.0f;
        float r = 0;
        float j = 0;
        while (!landed && j <= 32)
        {
            //for (j = 0; j <= currentJump * 2; j++)
            //{
            //if (landed) { }
            //else
            //{
            // Horizontal Check
            if (dir == Vector3.zero || clashed) { }
            else {
                ray = new Ray(nextpos
                    + (currentRun * r * dir * (1.0f / (currentJump * 2.0f)))
                    + ((sum / jumpsum) * currentJump * Vector3.up)
                    + new Vector3(0, myself.ObjectScale.y - 0.5f, 0), dir);
                distance = currentRun / (currentJump * 2);
                if (dir.x != 0 && dir.z != 0) { distance = distance * 1.5f; }
                if (Physics.Raycast(ray, out hit, distance))
                {
                    Debug.Log("hitfront");
                    clashed = true;
                }

            }
            if (clashed) { }
            else { r++; }

            // Vertical Check
            if (j < currentJump)
            {
                ray = new Ray(nextpos
                    + ((currentRun * r * (1.0f / (currentJump * 2.0f))) * dir)
                    + ((sum / jumpsum) * currentJump * Vector3.up)
                    + new Vector3(0, myself.ObjectScale.y - 0.5f, 0), Vector3.up);
                distance = Mathf.Abs(((currentJump - j) / jumpsum) * currentJump) + 0.5f;
                if (Physics.Raycast(ray, out hit, distance))
                {
                    Debug.Log("hitup");
                    j = currentJump;
                }
            }
            else if (j > currentJump)
            {
                ray = new Ray(nextpos
                    + ((currentRun * r * (1.0f / (currentJump * 2.0f))) * dir)
                    + ((sum / jumpsum) * currentJump * Vector3.up)
                    + new Vector3(0, 0.5f, 0), Vector3.down);
                distance = Mathf.Abs(((currentJump - j) / jumpsum) * currentJump) + 0.5f;
                if (Physics.Raycast(ray, out hit, distance))
                {
                    Debug.Log("landed");
                    if (myself.name == hit.collider.gameObject.name) { }
                    else
                    {
                        endpos = myself.RoundToIntVector3XZ(hit.point);
                        landed = true;
                    }
                }
            }

            // Fix and Add

            if (j == currentJump)
            {
                r--;
            }
            else if (landed)
            {
                jumpRouteList.Add(endpos);
            }
            else {
                sum += currentJump - j;
                jumpRouteList.Add(nextpos
                    + ((currentRun * r * (1.0f / (currentJump * 2.0f))) * dir)
                    + ((sum / jumpsum) * currentJump * Vector3.up));
            }
            //            }
            //            }
            j++;

        }
        if (landed) { }
        else { return (int)ErrorTypeList.Jump; }
        return (int)ErrorTypeList.Nothing;
    }