예제 #1
0
    protected override void Move()
    {
        if (GroundMovement != null)
        {
            Grounded = GroundMovement.TryStickToGround(false);
        }

        if (!Grounded)
        {
            Movement.AirMove();
        }
        base.Move();
    }
예제 #2
0
    protected virtual void Move()
    {
        // moving
        {
            Vector2 origin;
            float   boxHeight;
            //LayerMask GROUND_MASK = LayerMask.GetMask("Default");
            LayerMask GROUND_MASK = f_movement.Mask;

            // On the ground, the feet will be ignored, since they often walk through the floor a bit
            if (Grounded)
            {
                origin    = transform.TransformPoint(new Vector2(0, f_height * 0.75f));
                boxHeight = f_height / 2f * transform.localScale.y;
            }
            else     // in air
            {
                origin    = transform.TransformPoint(new Vector2(0, f_height * 0.55f));
                boxHeight = f_height * 0.9f * transform.localScale.y;

                //if (Vector2.Angle(transform.up, Velocity) > 45) {
                if (Velocity.y <= 0 || f_movement is AirMovement)
                {
                    boxHeight *= 0.9f;
                }
            }

            RaycastHit2D hit;

            // Trying to move
            // When there is a collision, the character only gets moved a bit and the velocity changes
            // It is then tried again if the character can move
            for (int i = 0; i < CONTROLLER_SO.MAX_COLLISION_ITERATIONS; ++i)
            {
                hit = Physics2D.BoxCast(origin, new Vector2(HalfWidth * 2f - 0.05f, boxHeight - 0.05f), transform.eulerAngles.z, Velocity, Velocity.magnitude / 60f, GROUND_MASK);

                if (hit)
                {
                    if (hit.fraction == 0)
                    {
                        if (Mathf.Abs(hit.point.x - transform.position.x) < 0.2f)
                        {
                            if (this is Player)
                            {
                                m_playerMovement += (Vector2.right * 0.1f * ((hit.transform.position.x < transform.position.x) ? 1 : -1)).Abs();
                            }
                            transform.position = transform.position + (Vector3)Vector2.right * 0.1f * ((hit.transform.position.x < transform.position.x) ? 1 : -1);
                            return;
                        }
                        else
                        {
                            // TODO; not sure about this one
                            // necessary but does some stupid stuff
                            GroundMovement?.TryStickToGround(true);
                            //transform.position = transform.position + (Vector3)(hit.normal * 0.1f + Velocity.normalized * 0.05f);
                        }
                    }
                    else
                    {
                        // updating the position by the fraction of the velocity which worked
                        if (this is Player)
                        {
                            m_playerMovement += ((hit.fraction - 0.1f) / 60f * Velocity).Abs();
                        }
                        transform.position = transform.position + (hit.fraction - 0.1f) / 60f * (Vector3)Velocity;
                    }



                    //Vector2 parallel = Vector2.Perpendicular(hit.normal);
                    Vector2 parallel = Vector3.Cross(hit.normal, Vector3.forward);

                    float parFraction = Vector2.Dot((1 - hit.fraction + 0.1f) / 60f * Velocity, parallel);

                    // this cancels out the Velocity in which the character is moving at the moment
                    Velocity = (60f * parFraction * parallel);

                    // the character will NOT move the rest of the movement here anymore; this will happen in the next iteration step
                }
                else
                {
                    // moving normally
                    if (this is Player)
                    {
                        m_playerMovement += (Velocity / 60f).Abs();
                    }
                    transform.position = transform.position + (Vector3)Velocity / 60f;

                    // Since the velocity has not been updated, this process can be ended
                    break;
                }
            }
        }

        Orient();

        f_animator.SetFloat("X", Velocity.x);
        f_animator.SetFloat("Y", Velocity.y);
    }