Beispiel #1
0
 // Start is called before the first frame update
 void Start()
 {
     move_script      = GetComponent <movePlayer>();
     combat_script    = GetComponent <CombatScript>();
     inventory_script = GetComponent <InventoryScript>();
     grab_script      = GetComponent <GrabScript>();
 }
Beispiel #2
0
    private void FixedUpdate()
    {
        //accumulates movement forces (walk, jump)
        Vector2    current_movement_force = new Vector2(0.0f, 0.0f);
        GrabScript grab_script            = GetComponent <GrabScript>();

        //clamp velocity
        if (rigidbody_2d.velocity.magnitude > air_max_velocity)
        {
            rigidbody_2d.velocity *= (1.0f / rigidbody_2d.velocity.magnitude) * air_max_velocity;
        }

        //get the horizontal input
        float moveHorizontal = move_input;

        //compute direction (only horizontal input)
        Vector3 direction = new Vector3(moveHorizontal, 0.0f, 0.0f);

        //flip player based on horizontal input
        if (Mathf.Abs(moveHorizontal) > 0.1f)
        {
            if (grab_script == null || grab_script.getGrabbedObject() == null)
            {
                transform.localScale = new Vector3(moveHorizontal < 0.0f ? 0.4f : -0.4f, 0.4f, 0.4f);
            }
        }

        //if there is movement input
        if (direction.magnitude < 0.0001f)
        {
            //slow down the horizontal velocity
            current_movement_force = new Vector2(-rigidbody_2d.velocity.x * move_slowdown_force, 0.0f);

            //set the animation state
            animator.SetInteger("WalkState", 0);
            animator.speed = 1.0f;
        }
        else
        {
            //scale max velocity to allow walking
            float walk_Scale = Mathf.Abs(direction.x);

            //compute boost scale
            float boost_scale = boost_time > 0.001f ? 1.5f : 1.0f;

            //compute scaling value to cancel out force on max speed
            float x = Mathf.Clamp(Mathf.Abs(rigidbody_2d.velocity.x) - move_max_velocity * boost_scale * walk_Scale, 0.0f, move_max_velocity_stride);
            x /= move_max_velocity_stride;
            x  = -(x * x) + 1.0f;

            if (direction.x * rigidbody_2d.velocity.x < 0)
            {
                x = 1.0f;
            }

            //slow down movement when in air
            x = current_jump_time > 0.01f ? x * move_air_scale : x;

            //add movement force
            current_movement_force = direction * move_force * x;

            //set the animation state
            animator.SetInteger("WalkState", (int)direction.normalized.x);
            animator.speed = 0.1f + Mathf.Abs(rigidbody_2d.velocity.x) * animation_speed_scale;
        }

        //jumping
        if (jump_input && !jump_cooldown && !jump_button_down)
        {
            //increment the jump time
            current_jump_time += Time.fixedDeltaTime;

            //add jump force or stop jumping if max jump time is reached
            if (current_jump_time >= jumping_max_time || rigidbody_2d.velocity.y > jump_max_y_velocity)
            {
                jump_cooldown    = true;
                jump_button_down = true;
            }
            else
            {
                current_movement_force.y += jumping_force;
            }
        }

        //if jump input is not set then release jump flag
        if (!jump_input)
        {
            jump_button_down = false;
            animator.SetInteger("JumpState", 0);
        }

        //play jump animation (to be added)
        if (current_jump_time > 0.01f)
        {
            animator.SetInteger("JumpState", 1);
        }

        // apply move force to grabbed object
        if (grab_script != null)
        {
            GameObject grabbed_object = grab_script.getGrabbedObject();
            if (grabbed_object != null)
            {
                Rigidbody2D rigidbody = grabbed_object.GetComponent <Rigidbody2D>();
                rigidbody.AddForce(new Vector3(current_movement_force.x, 0.0f, 0.0f));

                // attach player to grabbed object
                float distance = grab_script.getDistanceToOther(grabbed_object);

                Vector3 d = (grabbed_object.transform.position - transform.position).normalized;

                if (distance > 0.0f)
                {
                    rigidbody_2d.AddForce(new Vector3(d.x, 0.0f, 0.0f) * distance * current_movement_force.magnitude * 2.0f);
                }
            }
        }

        //add the force to the physics object
        rigidbody_2d.AddForce(current_movement_force);
    }