예제 #1
0
        public void TryRelease()
        {
            if (HeldGrabbable != null && HeldGrabbable.CanBeDropped)
            {
                GrabbableEventArgs args = new GrabbableEventArgs(HeldGrabbable.GetComponent <Grabbable>(), this);
                HeldGrabbable.DropItem(this);
                Drop?.Invoke(args);
            }

            // No longer try to bring flying grabbable to us
            resetFlyingGrabbable();
        }
예제 #2
0
        void attachMagazine()
        {
            // Drop Item
            var grabber = HeldMagazine.GetPrimaryGrabber();

            HeldMagazine.DropItem(grabber, false, false);

            // Play Sound
            VRUtils.Instance.PlaySpatialClipAt(ClipAttachSound, transform.position, 1f);

            // Move to desired location before locking in place
            moveMagazine(Vector3.zero);

            // Add fixed joint to make sure physics work properly
            if (transform.parent != null)
            {
                Rigidbody parentRB = transform.parent.GetComponent <Rigidbody>();
                if (parentRB)
                {
                    FixedJoint fj = HeldMagazine.gameObject.AddComponent <FixedJoint>();
                    fj.autoConfigureConnectedAnchor = true;
                    fj.axis          = new Vector3(0, 1, 0);
                    fj.connectedBody = parentRB;
                }

                // If attached to a Raycast weapon, let it know we attached something
                if (parentWeapon)
                {
                    parentWeapon.OnAttachedAmmo();
                }
            }

            // Don't let anything try to grab the magazine while it's within the weapon
            // We will use a grabbable proxy to grab the clip back out instead
            HeldMagazine.enabled = false;

            lockedInPlace   = true;
            magazineInPlace = true;
        }
예제 #3
0
        void Update()
        {
            if (!thisGrab.BeingHeld)
            {
                if (!didRelease)
                {
                    StartCoroutine(doRelease());
                    didRelease = true;
                }
            }
            else
            {
                // Object is being held, need to fire release
                didRelease = false;

                // Check Break Distance since we are always holding the helper
                if (thisGrab.BreakDistance > 0 && Vector3.Distance(transform.position, HandleTransform.position) > thisGrab.BreakDistance)
                {
                    thisGrab.DropItem(false, false);
                }
            }
        }
예제 #4
0
        void FixedUpdate()
        {
            if (!thisGrab.BeingHeld)
            {
                if (!didRelease)
                {
                    //col.enabled = false;
                    transform.localPosition = Vector3.zero;
                    transform.localRotation = Quaternion.identity;
                    transform.localScale    = Vector3.one;
                    rb.velocity             = Vector3.zero;
                    rb.angularVelocity      = Vector3.zero;

                    if (ParentRigid)
                    {
                        // ParentRigid.velocity = Vector3.zero;
                        // ParentRigid.angularVelocity = Vector3.zero;
                        ParentRigid.angularVelocity = lastAngularVelocity * 20;
                    }
                    col.enabled = true;
                    StartCoroutine(doRelease());

                    didRelease = true;
                }
            }
            else
            {
                // Object is being held, need to fire release
                didRelease = false;

                // Check Break Distance since we are always holding the helper
                if (thisGrab.BreakDistance > 0 && Vector3.Distance(transform.position, HandleTransform.position) > thisGrab.BreakDistance)
                {
                    thisGrab.DropItem(false, false);
                }

                lastAngularVelocity = rb.angularVelocity;
            }
        }
예제 #5
0
        public virtual void DestroyThis()
        {
            Health    = 0;
            destroyed = true;

            // Activate
            foreach (var go in ActivateGameObjectsOnDeath)
            {
                go.SetActive(true);
            }

            // Deactivate
            foreach (var go in DeactivateGameObjectsOnDeath)
            {
                go.SetActive(false);
            }

            // Colliders
            foreach (var col in DeactivateCollidersOnDeath)
            {
                col.enabled = false;
            }

            // Spawn object
            if (SpawnOnDeath != null)
            {
                var go = GameObject.Instantiate(SpawnOnDeath);
                go.transform.position = transform.position;
                go.transform.rotation = transform.rotation;
            }

            // Force to kinematic if rigid present
            if (rigid)
            {
                rigid.isKinematic = true;
            }

            // Invoke Callback Event
            if (onDestroyed != null)
            {
                onDestroyed.Invoke();
            }

            if (DestroyOnDeath)
            {
                Destroy(this.gameObject, DestroyDelay);
            }
            else if (Respawn)
            {
                StartCoroutine(RespawnRoutine(RespawnTime));
            }

            // Drop this if the player is holding it
            Grabbable grab = GetComponent <Grabbable>();

            if (DropOnDeath && grab != null && grab.BeingHeld)
            {
                grab.DropItem(false, true);
            }

            // Remove an decals that may have been parented to this object
            if (RemoveBulletHolesOnDeath)
            {
                BulletHole[] holes = GetComponentsInChildren <BulletHole>();
                foreach (var hole in holes)
                {
                    GameObject.Destroy(hole.gameObject);
                }

                Transform decal = transform.Find("Decal");
                if (decal)
                {
                    GameObject.Destroy(decal.gameObject);
                }
            }
        }
예제 #6
0
        private void OnCollisionEnter(Collision collision)
        {
            // Ignore parent collisions
            if (transform.parent != null && collision.transform == transform.parent)
            {
                return;
            }

            // Don't count collisions if being held
            if (grab != null && grab.BeingHeld)
            {
                return;
            }

            // Don't Count Triggers
            if (collision.collider.isTrigger)
            {
                return;
            }

            string colNameLower = collision.transform.name.ToLower();

            // Ignore other very close bows and arrows
            if (flightTime < 1 && (colNameLower.Contains("arrow") || colNameLower.Contains("bow")))
            {
                Physics.IgnoreCollision(collision.collider, ShaftCollider, true);
                return;
            }

            // ignore player collision if quick shot
            if (flightTime < 1 && collision.transform.name.ToLower().Contains("player"))
            {
                Physics.IgnoreCollision(collision.collider, ShaftCollider, true);
                return;
            }

            // Damage if possible
            float zVel    = System.Math.Abs(transform.InverseTransformDirection(rb.velocity).z);
            bool  doStick = true;

            if (zVel > 0.02f && !rb.isKinematic)
            {
                Damageable d = collision.gameObject.GetComponent <Damageable>();
                if (d)
                {
                    d.DealDamage(arrowDamage, collision.GetContact(0).point, collision.GetContact(0).normal, true, gameObject, collision.collider.gameObject);
                }

                // Don't stick to dead objects
                if (d != null && d.Health <= 0)
                {
                    doStick = false;
                }
            }

            // Check to stick to object
            if (!rb.isKinematic && Flying)
            {
                if (zVel > 0.02f)
                {
                    if (grab != null && grab.BeingHeld)
                    {
                        grab.DropItem(false, false);
                    }
                    if (doStick)
                    {
                        tryStickArrow(collision);
                    }

                    Flying = false;

                    playSoundInterval(2.462f, 2.68f);
                }
            }
        }