// updates camera button and camera position void LateUpdate() { CameraButton.Update(); // checks if camera button has been pressed if (CameraButton.IsButtonClicked()) { camMoving = true; trackPlayer = !trackPlayer; } if (camMoving) // if camera is still adjusting { IncrementMovingCamera(); } if (trackPlayer && !camMoving) { if (RopeSystem.IsRopeConnected()) { camMoving = true; } else { transform.position = player.transform.position + offset; } } }
// Update is called once per frame void Update() { // update the axisbutton utils first JumpButton.Update(); // Update the player position playerPos = transform.position; if (ControllerMode) { joystickPosition.x = Input.GetAxis(AimHorizontalAxis); joystickPosition.y = Input.GetAxis(AimVerticalAxis); } // Check that the ref to RopeSystem is not null, and the rope system is connected // the == true is not redundant because of the ?. operator // if this were expanded for multiple ropes, would just need to check that any are connected if (RopeSystem?.IsRopeConnected() == true) { AimingReticleObject.SetActive(false); UpdatePlayerSwingingForce(); } // Player is not suspended by a rope else { // Update the reticle endpoint and draw a line from the player to the endpoint UpdateAimingReticle(); UpdateGroundStrafingMovement(); //Jump if the player is on the ground and they just clicked the button if (JumpButton.IsButtonClicked() && onGround) { PlayerJump(); } } }
/// <summary> /// Update a rope that is being cast /// </summary> private void UpdateCastingRope() { // disable the distance joint when we are casting, for when the point // disables itself RopeDistanceJoint.enabled = false; //HookCollider.enabled = true; RopeAndHookCollider.enabled = true; HookSpriteObject.SetActive(true); // start throwing if not throwing already if (!IsCasting) { // fireSound.Play(); IsCasting = true; // reset the casting distance CurrentCastDistance = 0; } else { // increment the casting distance CurrentCastDistance += CastingSpeed * Time.deltaTime; // ensure it fits in the upper bound if (CurrentCastDistance > MaxCastDistance) { CurrentCastDistance = MaxCastDistance; } } // update the position of the line renderer // and the collider RopeLineRenderer.enabled = true; var directionVector = new Vector3(Mathf.Cos(AimAngle), Mathf.Sin(AimAngle), 0); var displayOrigin = transform.position + PlayerRopeDrawOffset; var displayOffset = new Vector3(CurrentCastDistance * directionVector.x, CurrentCastDistance * directionVector.y, 0); RopeLineRenderer.SetPosition(0, displayOrigin); RopeLineRenderer.SetPosition(1, transform.position + displayOffset); // get the midpoint var midPoint = Vector3.Lerp(Vector3.zero, displayOffset, 0.5f); var offset = new Vector2(midPoint.x, midPoint.y).magnitude; // set the center of the collider to the midpoint between the end of the cast RopeAndHookCollider.offset = new Vector2(0, CurrentCastDistance / 2); // size the collider to fit the cast RopeAndHookCollider.size = new Vector2(0.2f, CurrentCastDistance); // set the end of the sprite to the end of the grapple HookSpriteObject.transform.localPosition = CurrentCastDistance * directionVector; // rotate the sprite so that it looks correct HookSpriteObject.transform.rotation = Quaternion.Euler(0, 0, -90 + (Mathf.Rad2Deg * AimAngle)); // rotate the object that contains the casting collider transform.rotation = Quaternion.Euler(0, 0, -90 + (Mathf.Rad2Deg * AimAngle)); // if they just clicked the fire button if (FireButton.IsButtonClicked()) { // then invoke the on fire handler OnPlayerGrappleFire.Invoke(); } }
// updates camera button and camera position void LateUpdate() { CameraButton.Update(); // checks if camera button has been pressed if (CameraButton.IsButtonClicked()) { camMoving = true; trackPlayer = !trackPlayer; } if (camMoving) // if camera is still adjusting { // adjust move speed float step = camMoveSpeed * Time.deltaTime; if (trackPlayer) // camera tracking player { if (RopeSystem.IsRopeConnected()) { // moving towards swing point Rigidbody2D ropeConnect = RopeSystem.RopeAnchorPoint; if (playerCam.transform.position != ropeConnect.transform.position + ropeLockPos) { transform.position = Vector3.MoveTowards(transform.position, ropeConnect.transform.position + ropeLockPos, step); } else // reached position { camMoving = false; } } else // moving towards player position { float fastStep = camMoveSpeed * Time.deltaTime; // uses moveTowards to move camera Vector3 temp = player.transform.position + offset; if (playerCam.transform.position != temp) { transform.position = Vector3.MoveTowards( transform.position, temp, fastStep); } else // reached position { camMoving = false; } } } else // moves towards init static position { if (playerCam.transform.position != initPos) { transform.position = Vector3.MoveTowards( transform.position, initPos, step); } else // reached position { camMoving = false; } } } if (trackPlayer && !camMoving) { if (RopeSystem.IsRopeConnected()) { camMoving = true; } else { transform.position = player.transform.position + offset; } } }
// Update is called once per frame void Update() { // update the axisbutton utils first JumpButton.Update(); // Update the player position playerPos = transform.position; if (ControllerMode) { joystickPosition.x = Input.GetAxis(AimHorizontalAxis); joystickPosition.y = Input.GetAxis(AimVerticalAxis); } // Check that the ref to RopeSystem is not null, and the rope system is connected // the == true is not redundant because of the ?. operator // if this were expanded for multiple ropes, would just need to check that any are connected if (RopeSystem?.IsRopeConnected() == true) { AimingReticleObject.SetActive(false); // check the strafe axis var strafeAmount = Input.GetAxis(StrafeAxis); // get the perpendicular axis to the rope from the player var perpendicularAxis = RopeSystem.GetRopePerpendicularAxis().Value; // the direction of the perpendicular axis and where the player wants to go var directionForceAxis = strafeAmount * perpendicularAxis; // show some debug lines if debugging enabled if (ShowDebugging) { Debug.DrawRay(transform.position, directionForceAxis, Color.red); } // get the velocity of the player in this perpendicular axis (and in the direction they want to go) // if the player already has a large amount of velocity in this axis // then don't let them get more velocity in this axis var velocityInDirection = Vector2Project(PlayerRigidBody.velocity, directionForceAxis); // show some debug lines if debugging enabled if (ShowDebugging) { Debug.DrawRay(transform.position, velocityInDirection, Color.blue); } // check the velocity in the direction of motion to see if it is // not too large to apply some force to it // compare by magnitude if (velocityInDirection.magnitude < (MaxSwingingVelocity * directionForceAxis).magnitude) { // if the velocity was less than the max, then we can add some velocity in that direction // on this frame var toAdd = directionForceAxis * SwingForce * Time.deltaTime; PlayerRigidBody.AddForce(toAdd); if (ShowDebugging) { // Debug.Log(velocityInDirection.magnitude); Debug.DrawRay(transform.position, toAdd, Color.green); } } else { // Debug.Log("low " + velocityInDirection.magnitude); } } // Player is not suspended by a rope else { /* ~~ DISPLAY AIMING RETICLE ~~ */ //Debug.Log("joystickX " + joystickPosition.x + " joystickY: " + joystickPosition.y); float aimAngle; if (!ControllerMode) { // Get the mouse position, using a ray from the camera that hits a plane that the world is on ( Z = 0 ) // see here: https://answers.unity.com/questions/566519/camerascreentoworldpoint-in-perspective. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); // create a new plane at the origin that is facing forward Plane xy = new Plane(Vector3.forward, new Vector3(0, 0, 0)); float distance; xy.Raycast(ray, out distance); var rayPoint = ray.GetPoint(distance); var playerAimingLocation = rayPoint - playerPos; Debug.DrawLine(Vector3.zero, rayPoint); // Angle (radians) from the horizontal line going through the player to the mouse position aimAngle = Mathf.Atan2(playerAimingLocation.y, playerAimingLocation.x); } else { joystickPosition.x = Input.GetAxis(AimHorizontalAxis); joystickPosition.y = Input.GetAxis(AimVerticalAxis); // Calculate the angle based on joystick position. Invert y axis first aimAngle = Mathf.Atan2((0 - joystickPosition.y), joystickPosition.x); } // Keep it positive (e.g., straight below the player is 3pi/2 rad, not -pi/2) if (aimAngle < 0) { aimAngle = (Mathf.PI * 2) + aimAngle; } if (ShowDebugging) { Debug.Log("aimAngle: " + aimAngle + " rad"); } // Update the reticle endpoint and draw a line from the player to the endpoint UpdateReticlePosition(aimAngle); /* ~~ END DISPLAY AIMING RETICLE */ //Left and Right strafing movement float moveHorizontal = Input.GetAxis(StrafeAxis); if (ShowDebugging) { Debug.Log($"Axis: {StrafeAxis} Value: {moveHorizontal}"); } Vector2 movement = new Vector2(moveHorizontal, 0); PlayerRigidBody.AddForce(movement * StrafingForce * Time.deltaTime); //Jump if the player is on the ground and they just clicked the button if (JumpButton.IsButtonClicked() && onGround) { PlayerRigidBody.velocity = new Vector2(PlayerRigidBody.velocity.x, JumpingForce); onGround = false; } } }