Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- public class PsuedoJumper : MonoBehaviour
- {
- [Tooltip("The body that is being controlled to jump.")]
- public PsuedoBody2D PsuedoBody2D;
- [Tooltip("When the body lands a jump, this event will be executed.")]
- public GameEventController OnLandingEvent;
- [Tooltip("Normally, gravity is disabled during the jump and re-set to it's previous value upon finishing. If this is set, gravity will be turned enabled at landing regardless.")]
- public bool ForceGravityOnAtLanding = false;
- [Header("_TESTING_")]
- public bool TEST_Jump = false;
- public Vector3 TEST_Target = Vector3.zero;
- public float TEST_Speed;
- public float TEST_ArcHeight;
- public bool TEST_AllowJumpingInAir = true;
- public float TEST_TargetHeight;
- /// <summary>
- /// Whether or not the PsuedoJumper is currently jumping.
- /// </summary>
- public bool IsJumping { get; private set; }
- /// <summary>
- /// The height that the PsuedoBody2D is at.
- /// </summary>
- public float Height
- {
- get { return PsuedoBody2D.Height; }
- }
- #region Private fields
- /// <summary>
- /// The point in the world space that the body takes off from.
- /// </summary>
- private Vector3 _positionTakeOff;
- /// <summary>
- /// The point in the world space that the body will land on.
- /// </summary>
- private Vector3 _positionLanding;
- /// <summary>
- /// The total seconds the entire jump will take.
- /// </summary>
- private float _speed;
- /// <summary>
- /// The time that the jump started.
- /// </summary>
- private float _startTime;
- /// <summary>
- /// The parabolic function that represents the height of the body throughout the course of the jump.
- /// </summary>
- private Func<float, float> _parabolicFunction;
- /// <summary>
- /// Whether gravity was enabled or not for the PsuedoBody2D wehn the jump started.
- /// </summary>
- private bool _gravitySetting;
- #endregion
- void Start ()
- {
- if (PsuedoBody2D == null)
- {
- PsuedoBody2D = GetComponent<PsuedoBody2D>();
- }
- IsJumping = false;
- }
- void Update ()
- {
- if (TEST_Jump)
- {
- TEST_Jump = false;
- JumpToLocation(TEST_Target, TEST_ArcHeight, TEST_Speed, TEST_AllowJumpingInAir, TEST_TargetHeight);
- }
- if (!IsJumping)
- {
- return;
- }
- float t = _speed == 0 ? 1 : Mathf.Clamp01(((Time.time - _startTime)/_speed));
- Vector3 position = Vector3.Lerp(_positionTakeOff, _positionLanding, t);
- PsuedoBody2D.GroundPoint.position = position;
- float height = _parabolicFunction(t);
- PsuedoBody2D.Height = height;
- IsJumping = t < 1;
- if (IsJumping)
- {
- PsuedoBody2D.EnableGravity = false;
- }
- else
- {
- PsuedoBody2D.EnableGravity = ForceGravityOnAtLanding || _gravitySetting;
- GameEventController.Execute(OnLandingEvent);
- }
- }
- /// <summary>
- /// Jumps the PsuedoBody to the target location in the given number of seconds.
- /// </summary>
- /// <param name="target">Where to jump to.</param>
- /// <param name="arcHeightTip">The height of the arc used to jump.</param>
- /// <param name="speedInSeconds">How long to take for the jump.</param>
- /// <param name="allowAirborneJumping">If set to true, you can jump while already in the air, otherwise you must wait until the body is grounded.</param>
- /// <param name="targetHeight">The height that the jumper is trying to reach.</param>
- public void JumpToLocation(Vector3 target, float arcHeightTip, float speedInSeconds, bool allowAirborneJumping = false, float targetHeight = 0f)
- {
- if (PsuedoBody2D == null)
- {
- Debug.LogError("There is no PsuedoBody2D for this PsuedoJumper");
- return;
- }
- if(!allowAirborneJumping && !PsuedoBody2D.IsGrounded)
- {
- return;
- }
- _speed = speedInSeconds;
- _startTime = Time.time;
- _positionLanding = target;
- _positionTakeOff = PsuedoBody2D.GroundPoint.position;
- if (PsuedoBody2D.IsGrounded)
- {
- _gravitySetting = PsuedoBody2D.EnableGravity;
- }
- _parabolicFunction = GetParabolicFunction(PsuedoBody2D.Height, arcHeightTip, targetHeight);
- IsJumping = true;
- }
- /// <summary>
- /// Creates a function that represents the height of the PsuedoBody2D at a given percentage of it's journey.
- /// </summary>
- /// <param name="heightTakeOff">The height the body is at during takeoff.</param>
- /// <param name="heightArcTip">The tip of the arc for the travel.</param>
- /// <param name="heightLanding">The height of the landing.</param>
- /// <returns>Equation that when fed values from [0,1] returns the height the body should be at during that percentage of the journey.</returns>
- Func<float, float> GetParabolicFunction(float heightTakeOff, float heightArcTip, float heightLanding)
- {
- float a = 2 * heightTakeOff - 4 * heightArcTip + 2 * heightLanding;
- float b = -3 * heightTakeOff + 4 * heightArcTip - heightLanding;
- float c = heightTakeOff;
- return (i) => (a * i * i + b * i + c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment