/// <summary> /// Asynchronous function for moving an object to the position of the player's hand. /// </summary> /// <param name="animationTime">Duration of the movement (in seconds).</param> /// <param name="prop">A reference to the Holdable Object to move.</param> /// <returns>An enumerator</returns> private IEnumerator MovePropToHand(float animationTime, HoldableObjectV2 prop) { prop.onPickedUp.Invoke(); // Set the held object reference to the prop. holding = prop; // Create a Rigidbody2D prop body that is the rigidbody attached to the prop. Rigidbody2D propBody = prop.GetComponent <Rigidbody2D>(); // Set the body type of the prop body to kinematic. propBody.bodyType = RigidbodyType2D.Kinematic; // Set the velocity of the prop body to zero. propBody.velocity = Vector2.zero; // Loop the following for every step of stride length equal to 1 frame in seconds where: float t is between 0 and animation time. for (float t = 0; t < animationTime; t += Time.deltaTime) { // Set the position of the prop to the linear interpolation at position t on the line between the position of the prop, and the position of this object. prop.transform.position = Vector3.Lerp(prop.transform.position, hand.position, t); prop.transform.rotation = Quaternion.Slerp(prop.transform.rotation, hand.rotation, t); // Wait for 1 frame. yield return(new WaitForEndOfFrame()); } prop.transform.position = hand.position; prop.transform.rotation = hand.rotation; // Set the parent of the prop to the player's hand. prop.transform.parent = hand; }
/// <summary> /// Get the closest Holdable Object in the scene to the player, and pick it up. /// </summary> private void GrabNearestProp() { // If the player is already holding something, drop it. if (prop) { ReleaseHeldProp(); return; } // Create an array of Collider2D props that is the colliders of all props within reach of this player. Collider2D[] props = Physics2D.OverlapCircleAll(transform.position, maxReach, 1 << LayerMask.NameToLayer("Prop")); // If there are no props, stop algorythm. if (props.Length == 0) { return; } else { Debug.Log(props.Length); } // Create Holdable Object nearest that is the first prop. HoldableObjectV2 nearest = props[0].GetComponent <HoldableObjectV2>(); // For every prop in the list of props, foreach (Collider2D prop in props) { // If the distance between the prop and the player is less than the nearest prop and the player, if (Vector2.Distance(prop.transform.position, transform.position) < Vector2.Distance(nearest.transform.position, transform.position)) { // Set nearest to the prop. nearest = prop.GetComponent <HoldableObjectV2>(); } } // If nearest does not exist, stop the algorythm. if (nearest == null) { return; } // Asynchronously move the prop to the hand position. StartCoroutine(MovePropToHand(0.5f, nearest)); }
/// <summary> /// Drop any prop that is currently being held. /// </summary> private void ReleaseHeldProp() { // If the player is not holding anything, stop the algorythm. if (!prop) { return; } // Set the parent of the held object to null. prop.transform.parent = null; // Create Rigidbody2D prop body that is the rigidbody attached to the held object. Rigidbody2D propBody = prop.GetComponent <Rigidbody2D>(); // Set the body type of prop body to dynamic. propBody.bodyType = RigidbodyType2D.Dynamic; // Set the held object reference to null. prop = null; }
public void Hold(HoldableObjectV2 prop) { ReleaseHeldProp(); StartCoroutine(MovePropToHand(0.5f, prop)); }