Global physics properties and helper methods.

        /// <summary>
        /// Finds the IAudioInfluencer objects that are to be applied to the audio source.
        /// </summary>
        /// <returns>Collection of IAudioInfluencers between the user and the game object.</returns>
        private List<IAudioInfluencer> GetInfluencers()
        {
            List<IAudioInfluencer> influencers = new List<IAudioInfluencer>();
            Transform cameraTransform = CameraCache.Main.transform;

            // Influencers take effect only when between the emitter and the user.
            // Perform a raycast from the user toward the object.
            Vector3 direction = (gameObject.transform.position - cameraTransform.position).normalized;
            float distance = Vector3.Distance(cameraTransform.position, gameObject.transform.position);

            int count = UnityPhysics.RaycastNonAlloc(cameraTransform.position,
                                                direction,
                                                hits,
                                                distance,
                                                UnityPhysics.DefaultRaycastLayers,
                                                QueryTriggerInteraction.Ignore);
            
            for (int i = 0; i < count; i++)
            {
                IAudioInfluencer influencer = hits[i].collider.gameObject.GetComponentInParent<IAudioInfluencer>();
                if (influencer != null)
                {
                    influencers.Add(influencer);
                }
            }

            return influencers;
        }
Esempio n. 2
0
 static public int constructor(IntPtr l)
 {
     UnityEngine.Physics o;
     o = new UnityEngine.Physics();
     pushObject(l, o);
     return(1);
 }
Esempio n. 3
0
        private object CanBuild(Planner plan, Construction prefab)
        {
            if (prefab.prefabID != 2205372577)
            {
                return(null);
            }
            RaycastHit hit;
            BasePlayer player = plan.GetOwnerPlayer();

            if (permission.UserHasPermission(player.UserIDString, permissionName))
            {
                return(null);
            }
            if (!Physics.Raycast(player.eyes.HeadRay(), out hit, 5f))
            {
                return(null);
            }
            BaseEntity ent = hit.GetEntity();

            if (!ent)
            {
                return(null);
            }
            if (!ent.ShortPrefabName.Contains("external"))
            {
                return(null);
            }
            player.ChatMessage(msg("Deny Crusher", player.UserIDString));
            return(false);
        }
            public void FixedUpdate()
            {
                foreach (var boid in collection.Boids)
                {
                    if (UPhysics.Raycast(boid.Position, boid.Direction, anticipationDistance))
                    {
                        Debug.DrawLine(boid.Position, boid.Position + anticipationDistance * boid.Direction, Color.red);
                        foreach (var dir in maneuvers)
                        {
                            var globalDir = boid.Rotation * dir;

                            if (!UPhysics.SphereCast(new Ray(boid.Position, globalDir), 1f, maneuverDistance))
                            {
                                Debug.DrawLine(boid.Position, boid.Position + maneuverDistance * globalDir, Color.green);
                                boid.Acceleration += globalDir * intensity;
                                break;
                            }
                            else
                            {
                                Debug.DrawLine(boid.Position, boid.Position + maneuverDistance * globalDir, Color.red);
                            }
                        }
                    }
                    else
                    {
                        Debug.DrawLine(boid.Position, boid.Position + anticipationDistance * boid.Direction, Color.green);
                    }
                }
            }
        private void UpdateBounds()
        {
            if (cachedTargetCollider != null)
            {
                // Store current rotation then zero out the rotation so that the bounds
                // are computed when the object is in its 'axis aligned orientation'.
                Quaternion currentRotation = targetObject.transform.rotation;
                targetObject.transform.rotation = Quaternion.identity;
                UnityPhysics.SyncTransforms(); // Update collider bounds

                Vector3 boundsExtents = cachedTargetCollider.bounds.extents;

                // After bounds are computed, restore rotation...
                targetObject.transform.rotation = currentRotation;
                UnityPhysics.SyncTransforms();

                if (boundsExtents != Vector3.zero)
                {
                    if (flattenAxis == FlattenModeType.FlattenAuto)
                    {
                        float min = Mathf.Min(boundsExtents.x, Mathf.Min(boundsExtents.y, boundsExtents.z));
                        flattenAxis = min.Equals(boundsExtents.x) ? FlattenModeType.FlattenX : (min.Equals(boundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ);
                    }

                    boundsExtents.x      = flattenAxis == FlattenModeType.FlattenX ? 0.0f : boundsExtents.x;
                    boundsExtents.y      = flattenAxis == FlattenModeType.FlattenY ? 0.0f : boundsExtents.y;
                    boundsExtents.z      = flattenAxis == FlattenModeType.FlattenZ ? 0.0f : boundsExtents.z;
                    currentBoundsExtents = boundsExtents;

                    GetCornerPositionsFromBounds(new Bounds(Vector3.zero, boundsExtents * 2.0f), ref boundsCorners);
                    CalculateEdgeCenters();
                }
            }
        }
Esempio n. 6
0
        private void UpdateBounds()
        {
            if (TargetBounds != null)
            {
                // Store current rotation then zero out the rotation so that the bounds
                // are computed when the object is in its 'axis aligned orientation'.
                Quaternion currentRotation = Target.transform.rotation;
                Target.transform.rotation = Quaternion.identity;
                UnityPhysics.SyncTransforms(); // Update collider bounds

                Vector3 boundsExtents = TargetBounds.bounds.extents;

                // After bounds are computed, restore rotation...
                Target.transform.rotation = currentRotation;
                UnityPhysics.SyncTransforms();

                if (boundsExtents != Vector3.zero)
                {
                    if (flattenAxis == FlattenModeType.FlattenAuto)
                    {
                        float min = Mathf.Min(boundsExtents.x, Mathf.Min(boundsExtents.y, boundsExtents.z));
                        flattenAxis = (min == boundsExtents.x) ? FlattenModeType.FlattenX :
                                      ((min == boundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ);
                    }

                    boundsExtents.x      = (flattenAxis == FlattenModeType.FlattenX) ? 0.0f : boundsExtents.x;
                    boundsExtents.y      = (flattenAxis == FlattenModeType.FlattenY) ? 0.0f : boundsExtents.y;
                    boundsExtents.z      = (flattenAxis == FlattenModeType.FlattenZ) ? 0.0f : boundsExtents.z;
                    currentBoundsExtents = boundsExtents;

                    GetCornerPositionsFromBounds(new Bounds(Vector3.zero, boundsExtents * 2.0f), ref boundsCorners);
                    boundsUpdated.Invoke();
                }
            }
        }
        // Checks if the given position would collide with the ground collision layer.
        //      position: Position to check
        //      return: True if a ground collision would occur at the given position
        bool IsGroundCollision(Vector3 position)
        {
            // move sphere but to match bottom of character's capsule collider
            var colliderCount = UnityPhysics.OverlapSphereNonAlloc(position + new Vector3(0.0f, radius, 0.0f),
                                                                   radius, m_TrajectoryPredictionColliders,
                                                                   collisionLayerMask);

            return(colliderCount > 0.0f);
        }
Esempio n. 8
0
        protected virtual void UpdateFocus()
        {
            var direction = (Pivot - Transform.position).normalized;
            var ray       = new Ray(Pivot, direction);

            RaycastHit hit;

            var hasHit = UEPhysics.Raycast(
                ray, out hit, FocusSettings.MaximumDistance, FocusSettings.Layer);

            _focused.Value = hasHit ? hit.transform : null;
        }
	static public int constructor(IntPtr l) {
		try {
			UnityEngine.Physics o;
			o=new UnityEngine.Physics();
			pushValue(l,true);
			pushValue(l,o);
			return 2;
		}
		catch(Exception e) {
			return error(l,e);
		}
	}
Esempio n. 10
0
 static public int constructor(IntPtr l)
 {
     LuaDLL.lua_remove(l, 1);
     UnityEngine.Physics o;
     if (matchType(l, 1))
     {
         o = new UnityEngine.Physics();
         pushObject(l, o);
         return(1);
     }
     LuaDLL.luaL_error(l, "New object failed.");
     return(0);
 }
Esempio n. 11
0
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.Physics o;
         o = new UnityEngine.Physics();
         pushValue(l, o);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
        protected static CollisionFilter ProduceCollisionFilter(LegacyCollider collider)
        {
            var layer  = collider.gameObject.layer;
            var filter = new CollisionFilter {
                CategoryBits = (uint)(1 << collider.gameObject.layer)
            };

            for (var i = 0; i < 32; ++i)
            {
                filter.MaskBits |= (uint)(LegacyPhysics.GetIgnoreLayerCollision(layer, i) ? 0 : 1 << i);
            }
            return(filter);
        }
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.Physics o;
         o = new UnityEngine.Physics();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
        // Calculates the current predicted fall distance based on the predicted landing position
        //      return: The predicted fall distance
        float GetPredictedFallDistance()
        {
            RaycastHit hitInfo;

            if (UnityPhysics.Raycast(new Ray(footWorldPosition, Vector3.down), out hitInfo,
                                     m_GroundingDistance, collisionLayerMask))
            {
                return(hitInfo.distance);
            }

            UpdatePredictedLandingPosition();
            return(m_PredictedLandingPosition == null
                                ? float.MaxValue
                                : footWorldPosition.y - ((Vector3)m_PredictedLandingPosition).y);
        }
Esempio n. 15
0
            public static StorageContainer SpawnContainer(BasePlayer player, int size, bool die)
            {
                var pos = player.transform.position;

                if (die)
                {
                    RaycastHit hit;
                    if (Physics.Raycast(new Ray(player.GetCenter(), Vector3.down), out hit, 1000, rayColl, QueryTriggerInteraction.Ignore))
                    {
                        pos = hit.point;
                    }
                }
                else
                {
                    pos -= new Vector3(0, 100, 0);
                }
                return(SpawnContainer(player, size, pos));
            }
        private Vector3 CalculateBoundsExtents()
        {
            // Store current rotation then zero out the rotation so that the bounds
            // are computed when the object is in its 'axis aligned orientation'.
            Quaternion currentRotation = Target.transform.rotation;

            Target.transform.rotation = Quaternion.identity;
            UnityPhysics.SyncTransforms(); // Update collider bounds

            Vector3 boundsExtents = TargetBounds.bounds.extents;

            // After bounds are computed, restore rotation...
            Target.transform.rotation = currentRotation;
            UnityPhysics.SyncTransforms();

            // apply flattening
            return(VisualUtils.FlattenBounds(boundsExtents, flattenAxis));
        }
Esempio n. 17
0
        private static T GetLookEntity <T>(BasePlayer player)
        {
            RaycastHit hit;

            if (Physics.Raycast(player.eyes.HeadRay(), out hit) == false)
            {
                return(default(T));
            }

            var entity = hit.GetEntity();

            if (entity == null)
            {
                return(default(T));
            }

            return(entity.GetComponent <T>());
        }
Esempio n. 18
0
        private object CanBuild(Planner plan, Construction prefab)
        {
            if (prefab.prefabID != 2205372577)
            {
                return(null);
            }
            BasePlayer player = plan.GetOwnerPlayer();

            if (permission.UserHasPermission(player.UserIDString, permissionName))
            {
                return(null);
            }
            RaycastHit[] hits = Physics.RaycastAll(player.eyes.HeadRay(), 5f, collLayers);
            if (!hits.Where(hit => hit.GetEntity() != null).Any(hit => hit.GetEntity().ShortPrefabName.Contains("external")))
            {
                return(null);
            }
            player.ChatMessage(msg("Deny Crusher", player.UserIDString));
            return(false);
        }
Esempio n. 19
0
        /// <summary>
        /// Performs a CapsuleCast from a specified collider at an offset position in a direction.
        /// Outputs a RaycastHit.
        /// Returns a bool that is true if something was hit.
        /// </summary>
        /// <param name="collider">Collider to model CapsuleCast from.</param>
        /// <param name="offset">Offset position to apply to collider position for CapsuleCast.</param>
        /// <param name="direction">Direction of CapsuleCast.</param>
        /// <param name="layerMask">Layers to hit.</param>
        /// <param name="hit">Outputted RaycastHit struct.</param>
        /// <param name="invertLayerMask">If true, invert layer mask. Otherwise, use layer mask as normal.</param>
        /// <returns>Returns true if the CapsuleCast hit something. Otherwise, returns false. When false, outputted hit is null.</returns>
        public static bool CapsuleCast(
            CapsuleCollider collider,
            Vector3 offset,
            Vector3 direction,
            LayerMask layerMask,
            out RaycastHit hit,
            bool invertLayerMask = false
            )
        {
            Vector3 capStart, capEnd;

            CalculateCapsuleCaps(collider, collider.transform.position + offset, collider.transform.rotation, out capStart, out capEnd);
            return(UPhysics.CapsuleCast(
                       capStart,
                       capEnd,
                       collider.radius,
                       direction.normalized,
                       out hit,
                       direction.magnitude,
                       invertLayerMask ? ~layerMask.value : layerMask.value
                       ));
        }
        private void SetBoundsControlCollider()
        {
            // Make sure that the bounds of all child objects are up to date before we compute bounds
            UnityPhysics.SyncTransforms();

            if (boundsOverride != null)
            {
                TargetBounds = boundsOverride;
                TargetBounds.transform.hasChanged = true;
            }
            else
            {
                Bounds bounds = GetTargetBounds();
                TargetBounds = Target.AddComponent <BoxCollider>();

                TargetBounds.center = bounds.center;
                TargetBounds.size   = bounds.size;
            }

            CalculateBoxPadding();
            TargetBounds.EnsureComponent <NearInteractionGrabbable>();
        }
Esempio n. 21
0
    private void DetectObjectCollision()
    {
        var collisions = Physics.OverlapSphere(transform.position, 1.5f, grabbiesLayerMask);

        DebugExtensions.DebugWireSphere(transform.position, Color.black, 1.5f, 2f);

        if (collisions.Length == 0)
        {
            return;
        }

        var mergeableObject = collisions[0].GetComponentInParent <MergeableObject>();

        if (currentMergeableObject == null)
        {
            currentMergeableObject = mergeableObject;
            mergeableObject.transform.SetParent(grabbyPoint);
            mergeableObject.transform.localPosition = Vector3.zero;
            mergeableObject.transform.localRotation = Quaternion.identity;
            mergeableObject.transform.localScale    = Vector3.one;
            collisions[0].enabled = false;
            mergeableObject.GetComponent <FakeGravity>().enabled = false;

            AudioManager.Instance.PlayCombineSuccess();
            //Close hands
        }
        else
        {
            if (currentMergeableObject.UpgradeObjectByAddition(mergeableObject.VectorMaterial))
            {
                AudioManager.Instance.PlayCombineSuccess();
                Destroy(mergeableObject.gameObject);
            }
            else
            {
                AudioManager.Instance.PlayCombineFail();
            }
        }
    }
Esempio n. 22
0
        protected virtual float GetUnblockedDistance(float distance)
        {
            if (!WallAvoidanceSettings.AvoidWalls)
            {
                return(distance);
            }

            var direction = (Transform.position - Pivot).normalized;
            var origin    = Pivot + direction * DistanceSettings.Minimum;

            var ray = new Ray(origin, direction);

            RaycastHit hit;

            if (UEPhysics.Raycast(ray, out hit, distance))
            {
                distance =
                    Vector3.Distance(Pivot, hit.point) -
                    WallAvoidanceSettings.MinimumDistance;
            }

            return(distance);
        }
Esempio n. 23
0
 public static int constructor(IntPtr l)
 {
     try {
         UnityEngine.Physics o;
         o=new UnityEngine.Physics();
         pushValue(l,o);
         return 1;
     }
     catch(Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return 0;
     }
 }
Esempio n. 24
0
 public bool CastRay(Ray ray, out RaycastHit hit, float maxDistance, int layerMask = UPhysics.AllLayers)
 {
     return(UPhysics.Raycast(ray, out hit, maxDistance, layerMask));
 }