Example #1
0
    //Fixed update is where all movement inputs are accepted, and the forces on the players rigidbody are updated
    void FixedUpdate()
    {
        // Jumping
        if (Input.GetAxis("Jump") > 0 && IsGrounded())
        {
            jumping    = true;
            standingOn = null;
        }
        // Apply movement velocity
        //Apply full speed in desired direction if grounded
        if (IsGrounded())
        {
            r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * maxSpeed, r2d.velocity.y);
        }
        //Else if we arent grounded, add aircontrol force in desired direction
        else
        {
            r2d.AddForce(new Vector2(Input.GetAxis("Horizontal") * airControlStrength, 0));
        }
        //Cap horizontal speed to max speed
        if (Mathf.Abs(r2d.velocity.x) > maxSpeed)
        {
            r2d.velocity = new Vector2((maxSpeed * Mathf.Sign(r2d.velocity.x)), r2d.velocity.y);
        }

        //Add jump force if jump has been triggered
        if (jumping)
        {
            //Update the animator
            animator.SetBool("Jumping", true);

            jumpSound.Play();
            //Jump goes negative if gravity on player is reversed
            if (gravityObject.GetGravityState())
            {
                r2d.velocity = new Vector2(r2d.velocity.x, -jumpHeight);
            }
            else
            {
                r2d.velocity = new Vector2(r2d.velocity.x, jumpHeight);
            }
            jumping = false;
        }

        //Update players facing (before factoring movement of object that the player is standing on)
        UpdateOrientation();

        //Update our final speed with the speed of the object we're standing on if required
        if (IsGrounded() && standingOn != null)
        {
            //Only update if there is a significant amount of movement (hack to avoid weird twitching when standing on rigidbodies)
            if (Mathf.Abs(standingOn.velocity.x) > 0.01f)
            {
                r2d.velocity = new Vector2(r2d.velocity.x + standingOn.velocity.x, r2d.velocity.y);
            }
        }
    }
Example #2
0
    void Update()
    {
        //Draw line showing the grab range
        //DrawDebugLine();
        //Pressing the grab button
        if (Input.GetKeyUp(KeyCode.C))
        {
            Debug.Log("Grab button pressed");

            Physics2D.queriesStartInColliders = false;

            if (!grabbing)
            {
                RaycastHit2D toGrab;
                //Decide on grab direction based on gravity state
                if (playerGravObject.GetGravityState())
                {
                    toGrab = Physics2D.Raycast(transform.position, Vector2.left * transform.localScale.x, GrabRange, grabLayer);
                }
                else
                {
                    toGrab = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, GrabRange, grabLayer);
                }

                if (toGrab.transform != null)
                {
                    Debug.Log("Grabbable object hit");
                    GrabObject(toGrab.transform.gameObject);
                }
            }
            //Logic to release grabbed object on second key press
            else
            {
                ReleaseGrab();
            }
        }

        //Check current forces and stop the grab if forces are too high
        //CheckForce();
    }
Example #3
0
 internal virtual void Move()
 {
     isGrounded = _isGrounded;
     if (IsActive())
     {
         jumpTimeElapsed = isGrounded ? jumpGracePeriod : jumpTimeElapsed - Time.deltaTime;
         gravityObj.SetRigidbodyGravity((isGrounded && !hasJumped) || (isGrounded && isCollided) ? GravityState.GroundedGravity : gravityObj.GetGravityState());
         //rb.gravityScale = (isGrounded && !hasJumped) || (isGrounded && isCollided) ? 0 : gravityObj.GetCurrentGravity();
         if (input != Vector2.zero)
         {
             currentMovementMultiplier = isGrounded ? 1 : airControlPercent;
             rb.AddForce(movementVector * totalSpeedMultiplier * Time.deltaTime, ForceMode2D.Force);
         }
         else
         {
             rb.AddForce(-rb.velocity.x * Vector2.right * totalSpeedMultiplier * stoppingPower * Time.deltaTime);
         }
     }
 }