About this mod
U7 compatible! It's dangerous to go alone, take this! Link's boomerang from Ocarina of Time. Throw it and it will return to you.
- Permissions and credits
- Changelogs
namespace Boomerang{ // The item module will add a unity component to the item object. See unity monobehaviour for more information: https://docs.unity3d.com/ScriptReference/MonoBehaviour.html // This component will apply a force on the player rigidbody to the direction of an item transform when the trigger is pressed (see custom reference in the item definition component of the item prefab) public class ItemBoomerang : MonoBehaviour { protected Item item; private Transform boomerangTransform; private Transform headTransform;
public ItemModuleBoomerang module;
private Vector3 returnDirection;
private bool isSpinning = false; private bool isReturning = false; private bool firstTrow = true; private bool reachedBubble = false;
private float bubbleTimer = 0f; private float bubbleTimeOut = 1f;
protected void Awake() { item = this.GetComponent<Item>(); headTransform = Player.local.head.gameObject.transform; boomerangTransform = item.transform;
module = item.data.GetModule<ItemModuleBoomerang>();
item.OnCollisionEvent += OnBoomerangCollisionEvent; item.OnGrabEvent += OnBoomerangGrabEvent; item.OnTeleGrabEvent += OnBoomerangTeleGrab; item.OnUngrabEvent += OnBoomerangUngrabEvent; }
void OnBoomerangCollisionEvent(ref CollisionStruct collisionInstance) {
StopSpinning(); }
void OnBoomerangTeleGrab(Handle handle, Telekinesis teleGrabber) {
StopSpinning();
}
void OnBoomerangGrabEvent(Handle handle, Interactor interactor) { StopSpinning(); }
void OnBoomerangUngrabEvent(Handle handle, Interactor interactor, bool throwing) { StartSpinning(); }
void FixedUpdate() {
if (isSpinning) { boomerangTransform.Rotate(Vector3.forward, module.spinningSpeed); if(item.rb.velocity.magnitude <= 3f && !isReturning) { StartReturning(); } }
if (isReturning) { if( Vector3.Distance(boomerangTransform.position, headTransform.position) <= module.returningBubble) {
reachedBubble = true; }
if(item.rb.velocity.magnitude < module.maxSpeed) { item.rb.AddForce(returnDirection * module.returnSpeed, ForceMode.Acceleration); }
if (reachedBubble) { bubbleTimer += Time.deltaTime; }
if(bubbleTimer >= bubbleTimeOut) { StopSpinning(); }
}
}
void StopSpinning() { isSpinning = false; item.rb.useGravity = true; isReturning = false; firstTrow = true; bubbleTimer = 0f; reachedBubble = false; }
void StartSpinning() { if(item.rb.velocity.magnitude > module.minSpeedForSpinning) { isSpinning = true; item.rb.useGravity = false; }
}
void StartReturning() { if (firstTrow) { firstTrow = false; isReturning = true; returnDirection = headTransform.position - boomerangTransform.position; returnDirection.Normalize(); }
} }