Ejemplo n.º 1
0
 private void FixedUpdate()
 {
     if (!moving)
     {
         ani.SetInteger("WalkDir", 0);
         shouldMove();
         moveNow -= Time.deltaTime;
     }
     else
     {
         om.Move(dirMove);
         shouldStop();
         moveNow -= Time.deltaTime;
     }
     CreateFootPrint();
 }
Ejemplo n.º 2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (Vector2.Distance(transform.position, playerPos) <= 5)
        {
            if (!entered)
            {
                if (speed == 0 && Vector3.Dot(Vector2.up, playerPos - transformOrigin) >= .8f)
                {
                    speed = defaultSpeed;
                }
                else if (speed != 0 && Vector3.Dot(Vector2.down, playerPos - transformOrigin) >= .6f)
                {
                    direction *= -1;
                    timer      = change - timer;
                }
                entered = true;
            }
        }
        else
        {
            if (speed == 0 && Vector3.Dot(direction, playerPos - center) >= 0.5f)
            {
                speed = defaultSpeed;
            }
            else if (speed != 0 && Vector3.Dot(-direction, playerPos - center) >= 0.5f)
            {
                direction *= -1;
                timer      = change - timer;
            }

            entered = false;
        }



        if (speed != 0)
        {
            timer += Time.fixedDeltaTime;
        }
        if (timer >= change)
        {
            timer      = 0;
            speed      = 0;
            direction *= -1f;
        }
        mover.Move(speed * direction * Time.fixedDeltaTime);

        if (direction.y < 0)
        {
            bool prevQueriesHitTriggers = Physics2D.queriesHitTriggers;
            Physics2D.queriesHitTriggers = true;

            int            maxSize = 10;
            RaycastHit2D[] hits    = new RaycastHit2D[maxSize];
            int            numHits;
            if ((numHits = col.Cast(-direction, hits, Physics2D.defaultContactOffset + (speed * Time.fixedDeltaTime))) > 0)
            {
                if (numHits > maxSize)
                {
                    numHits = maxSize;
                }

                for (int i = 0; i < numHits; i++)
                {
                    if (hits[i].collider.gameObject.tag == "Player" || hits[i].collider.gameObject.tag == "Enemy")
                    {
                        Character character = hits[i].collider.gameObject.GetComponent <Character>();
                        int       contactCount;
                        if (character.movement.charCont.isGrounded && character.movement.charCont.CheckGroundableSlope(-hits[i].normal))
                        {
                            ;
                        }
                        {
                            if (!groundedCharacters.Contains(character))
                            {
                                groundedCharacters.Add(character);
                            }
                        }
                    }
                }
            }
            Physics2D.queriesHitTriggers = prevQueriesHitTriggers;
        }

        foreach (Character character in groundedCharacters)
        {
            int        moveCount;
            MoveData[] moveData = character.movement.charCont.Move(speed * direction * Time.fixedDeltaTime, out moveCount, true);
        }
        groundedCharacters.Clear();
    }
Ejemplo n.º 3
0
    public MoveData[] MoveAlongGround(Vector2 moveBy, out int moveCount)
    {
        isTouchingCeiling   = false;
        isTouchingRightWall = false;
        isTouchingLeftWall  = false;

        if (moveBy == Vector2.zero)
        {
            moveCount = 0;
            return(new MoveData[0]);
        }
        int maxSize = 10;

        MoveData[] moveDatas = new MoveData[maxSize];
        Vector2    newMoveBy = moveBy;
        int        loops     = 0;

        moveCount = 1;
        bool completed = false;

        while (true)
        {
            completed = (moveDatas[moveCount - 1] = mover.Move(newMoveBy)).moveCompleted;

            isTouchingCeiling   = false;
            isTouchingRightWall = false;
            isTouchingLeftWall  = false;

            bool    hitWall     = false;
            Vector2 slopeNormal = Vector2.zero;
            int     size        = moveDatas[moveCount - 1].contactCount;
            bool    stepHit     = false;
            for (int i = 0; i < size; i++)
            {
                ContactData contact = moveDatas[moveCount - 1].contacts[i];

                if (CheckGroundableSlope(contact.normal) && !stepHit)
                {
                    slopeNormal += contact.normal;
                }
                else
                {
                    if (contact.wasHit)
                    {
                        hitWall = true;
                    }
                    if (contact.isCorner && CheckStep(contact))
                    {
                        slopeNormal = contact.normal;//HandleStep(contact);
                        stepHit     = true;
                    }
                    else
                    {
                        if (Vector2.Dot(contact.normal, Vector2.down) > 0.000001f)
                        {
                            isTouchingCeiling = true;
                        }

                        float dotLeft = Vector2.Dot(contact.normal, Vector2.left);
                        if (dotLeft > Mathf.Epsilon)
                        {
                            isTouchingRightWall = true;
                        }
                        else if (dotLeft < -Mathf.Epsilon)
                        {
                            isTouchingLeftWall = true;
                        }
                    }
                }
            }
            if (slopeNormal != Vector2.zero)
            {
                SetSlope(slopeFromNormal(slopeNormal.normalized));
            }
            else
            {
                if (!AttemptReground())
                {
                    Unground();
                    break;
                }
            }
            if (hitWall || isGrounded)
            {
                break;
            }


            newMoveBy = Mathf.Sign(Vector2.Dot(moveDatas[moveCount - 1].moveRemaining, currentSlope))
                        * moveDatas[moveCount - 1].moveRemaining.magnitude * currentSlope;

            moveCount++;

            loops++;
            if (loops > maxSize)
            {
                Debug.LogError("Possible Infinite Loop. Exiting");
                break;
            }
        }

        return(moveDatas);
    }
Ejemplo n.º 4
0
 private void MoveObject()
 {
     velocity.x = SpeedX.Value;
     velocity.y = SpeedY.Value;
     movedObject.Move(velocity * Time.deltaTime);
 }
Ejemplo n.º 5
0
    void CalculateMovement()
    {
        moving = false;
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        movedir = (int)ObjectMover.DIR.NONE;

        if (h > 0)
        {
            // right?
            movedir     += (int)ObjectMover.DIR.RIGHT;
            walkingRight = true;
        }
        else if (h < 0)
        {
            // left?
            movedir     += (int)ObjectMover.DIR.LEFT;
            walkingRight = false;
        }

        if (v > 0)
        {
            // up?
            movedir += (int)ObjectMover.DIR.UP;
        }
        else if (v < 0)
        {
            // down?
            movedir += (int)ObjectMover.DIR.DOWN;
        }

        if (movedir != (int)ObjectMover.DIR.NONE)
        {
            moving = true;
            om.Move((ObjectMover.DIR)movedir);
        }

        Vector3 scale = transform.localScale;

        if (walkingRight)
        {
            scale.x = -1;
        }
        else
        {
            scale.x = 1;
        }
        transform.localScale = scale;

        sound.setWalkingLoop(moving);

        if (moving)
        {
            // dosomething later
        }
        else
        {
            ani.SetInteger("WalkDir", 0);
        }
    }