Vector3 PetFaceDirectionToVector(PetFaceDirection faceDirection) { switch (faceDirection) { case PetFaceDirection.UP: return(transform.up); case PetFaceDirection.DOWN: return(-transform.up); case PetFaceDirection.LEFT: return(-transform.right); case PetFaceDirection.RIGHT: return(transform.right); case PetFaceDirection.FORWARD: return(transform.forward); case PetFaceDirection.BACK: return(-transform.forward); } throw new Exception("Invalid facedirection: " + faceDirection.ToString()); }
/// <summary> /// Finds the closest cube face to the given position, excluding the ones facing the top and bottom /// </summary> /// <param name="position"></param> /// <returns>PetFaceDirection representing the face side</returns> PetFaceDirection GetClosestFacePosition(Vector3 position) { Vector3 closestDirection = Vector3.zero; PetFaceDirection closestFaceDirection = 0; // highest dot product of direction vector to position is most in line with item float highestDotProduct = -1; // iterate all the values of PetFaceDirection (up,down,left, right etc) for (int i = 0; i < System.Enum.GetValues(typeof(PetFaceDirection)).Length; i++) { PetFaceDirection petFaceDirection = (PetFaceDirection)i; // convert a face direction to a world space direction from that face Vector3 potentialDirection = PetFaceDirectionToVector(petFaceDirection); // If this direction points up or down, skip it if (Vector3.Dot(potentialDirection, Vector3.up) > 0.5f) { continue; } if (Vector3.Dot(potentialDirection, Vector3.down) > 0.5f) { continue; } float dot = Vector3.Dot(potentialDirection, position - this.transform.position); if (dot > highestDotProduct) { highestDotProduct = dot; closestFaceDirection = petFaceDirection; closestDirection = potentialDirection; } } return(closestFaceDirection); }
void FixedUpdate() { if (isHoldingObject) { // if the holding face is pointing up or down, find a horizontal one // for example, if the cube fell over whilst holding if (Vector3.Dot(_holdDirection, Vector3.up) > 0.5f || Vector3.Dot(_holdDirection, Vector3.down) > 0.5f) { _heldFaceDirection = GetClosestFacePosition(currentHeldObject.transform.position); } Vector3 targetPoint = this.transform.position + new Vector3(0, _holdYOffset, 0) + (_holdDirection * _holdDistance); Vector3 force = targetPoint - currentHeldObject.transform.position; //if (force.magnitude > 1) force.Normalize(); force *= _holdForce; currentHeldObject.GetComponent <Rigidbody>().AddForce(force); Debug.DrawLine(this.transform.position, targetPoint, Color.red); } }
public void PickUpObject(FoodObject food) { if (currentHeldObject != null) { if (currentHeldObject == food) { return; // already holding this food } else { DropObject(); // drop whatever was being held before } } currentHeldObject = food; _prevObjectDrag = food.GetComponent <Rigidbody>().drag; food.GetComponent <Rigidbody>().drag = 10f; // stop it from osciliating when being picked up _heldFaceDirection = GetClosestFacePosition(food.transform.position); _holdDistance = 0.3f; StartCoroutine(PickUpObjectCoroutine()); }