// for backward compatible private void SetLagacyRaycastMode(RaycastMode mode) { switch (mode) { case RaycastMode.Projection: { var gen = ForceEnableSegmentGenerator <ProjectionGenerator>(); gen.velocity = m_velocity; gen.gravity = m_gravity; break; } case RaycastMode.Projectile: { var gen = ForceEnableSegmentGenerator <ProjectileGenerator>(); gen.velocity = m_velocity; gen.gravity = m_gravity; break; } default: { ForceDisableSegmentGenerators(); break; } } }
/// <summary> /// Updates type of raycast and enables correct cursor. /// </summary> private void UpdateRaycastMode() { _raycastMode = RaycastMode.Eyes; // Default all objects to inactive and then set active to the appropriate ones. _raycastEyes.gameObject.SetActive(true); }
/// <summary> /// Calculates the ray for the segment of the Hybrid raycast determined by the raycast mode /// passed in. /// </summary> /// <remarks> /// Throws an exception if Hybrid is passed in. /// </remarks> /// <param name="pointer">Which pointer to project the ray from.</param> /// <param name="hybridMode"> /// Which Raycast sub-mode to use within Hybrid mode. Must be Camera or Direct. /// </param> /// <returns>The PointerRay as projected from the GvrbasePointer in the given mode.</returns> public static PointerRay CalculateHybridRay(GvrBasePointer pointer, RaycastMode hybridMode) { PointerRay result; switch (hybridMode) { case RaycastMode.Direct: result = CalculateRay(pointer, hybridMode); result.distance = pointer.CameraRayIntersectionDistance; break; case RaycastMode.Camera: result = CalculateRay(pointer, hybridMode); PointerRay directRay = CalculateHybridRay(pointer, RaycastMode.Direct); result.ray.origin = directRay.ray.GetPoint(directRay.distance); result.distanceFromStart = directRay.distance; result.distance = pointer.MaxPointerDistance - directRay.distance; break; default: throw new UnityException( "Invalid RaycastMode " + hybridMode + " passed into CalculateHybridRay."); } return(result); }
void UpdateRaycastMode() { Vector3 forward = RaycastStartPoint.transform.forward; Vector3 startPos = RaycastStartPoint.transform.position; Physics.Raycast(startPos, forward, out hit); if (hit.collider) { ViveSR_StaticColliderInfo cldInfo = hit.collider.gameObject.GetComponent <ViveSR_StaticColliderInfo>(); if (CheckHorizontalValidHit(hit, cldInfo)) { raycastMode = RaycastMode.ValidHit_Horizontal; hitCldInfo = cldInfo; } else if (CheckValidHit(hit, cldInfo)) { raycastMode = RaycastMode.ValidHit; hitCldInfo = cldInfo; } else { raycastMode = RaycastMode.InvalidHit; } } else { raycastMode = RaycastMode.NoHit; } }
/// <summary> /// Handles the event for button down and cycles the raycast mode. /// </summary> /// <param name="controllerId">The id of the controller.</param> /// <param name="button">The button that is being pressed.</param> private void OnButtonDown(byte controllerId, MLInput.Controller.Button button) { if (_controllerConnectionHandler.IsControllerValid(controllerId) && button == MLInput.Controller.Button.Bumper) { _raycastMode = (RaycastMode)((int)(_raycastMode + 1) % _modeCount); UpdateRaycastMode(); } }
/// <summary> /// Calculates the ray for a given Raycast mode. /// </summary> /// <remarks> /// Will throw an exception if the raycast mode Hybrid is passed in. /// If you need to calculate the ray for the direct or camera segment of the Hybrid raycast, /// use CalculateHybridRay instead. /// </remarks> /// <param name="pointer">Which pointer to project the ray from.</param> /// <param name="mode">Which Raycast mode to use. Must be Camera or Direct.</param> /// <returns>The PointerRay as projected from the GvrbasePointer in the given mode.</returns> public static PointerRay CalculateRay(GvrBasePointer pointer, RaycastMode mode) { PointerRay result = new PointerRay(); if (pointer == null || !pointer.IsAvailable) { Debug.LogError("Cannot calculate ray when the pointer isn't available."); return(result); } Transform pointerTransform = pointer.PointerTransform; if (pointerTransform == null) { Debug.LogError("Cannot calculate ray when pointerTransform is null."); return(result); } result.distance = pointer.MaxPointerDistance; switch (mode) { case RaycastMode.Camera: Camera camera = pointer.PointerCamera; if (camera == null) { Debug.LogError("Cannot calculate ray because pointer.PointerCamera is null." + "To fix this, either tag a Camera as \"MainCamera\" or set " + "overridePointerCamera."); return(result); } Vector3 rayPointerStart = pointerTransform.position; Vector3 rayPointerEnd = rayPointerStart + (pointerTransform.forward * pointer.CameraRayIntersectionDistance); Vector3 cameraLocation = camera.transform.position; Vector3 finalRayDirection = rayPointerEnd - cameraLocation; finalRayDirection.Normalize(); Vector3 finalRayStart = cameraLocation + (finalRayDirection * camera.nearClipPlane); result.ray = new Ray(finalRayStart, finalRayDirection); break; case RaycastMode.Direct: result.ray = new Ray(pointerTransform.position, pointerTransform.forward); break; default: throw new UnityException( "Invalid RaycastMode " + mode + " passed into CalculateRay."); } return(result); }
/// <summary> /// Handles the event for button down and cycles the raycast mode. /// </summary> /// <param name="controller_id">The id of the controller.</param> /// <param name="button">The button that is being pressed.</param> private void OnButtonDown(byte controller_id, MLInputControllerButton button) { if (button == MLInputControllerButton.Bumper) { _raycastMode = (RaycastMode)((int)(_raycastMode + 1) % _modeCount); UpdateRaycastMode(); UpdateStatusText(); } }
public object Raycast(Ray3 ray, RaycastMode mode, bool raycastTerrain = true, bool raycastBodies = true, bool raycastMovingBlocks = true) { float reach = (m_subsystemGameInfo.WorldSettings.GameMode == GameMode.Creative) ? SettingsManager.CreativeReach : 5f; Vector3 creaturePosition = ComponentCreature.ComponentCreatureModel.EyePosition; Vector3 start = ray.Position; Vector3 direction = Vector3.Normalize(ray.Direction); Vector3 end = ray.Position + direction * 15f; Point3 startCell = Terrain.ToCell(start); BodyRaycastResult? bodyRaycastResult = m_subsystemBodies.Raycast(start, end, 0.35f, (ComponentBody body, float distance) => (Vector3.DistanceSquared(start + distance * direction, creaturePosition) <= reach * reach && body.Entity != base.Entity && !body.IsChildOfBody(ComponentCreature.ComponentBody) && !ComponentCreature.ComponentBody.IsChildOfBody(body) && Vector3.Dot(Vector3.Normalize(body.BoundingBox.Center() - start), direction) > 0.7f) ? true : false); MovingBlocksRaycastResult?movingBlocksRaycastResult = m_subsystemMovingBlocks.Raycast(start, end, extendToFillCells: true); TerrainRaycastResult? terrainRaycastResult = m_subsystemTerrain.Raycast(start, end, useInteractionBoxes : true, skipAirBlocks : true, delegate(int value, float distance) { if (Vector3.DistanceSquared(start + distance * direction, creaturePosition) <= reach * reach) { Block block = BlocksManager.Blocks[Terrain.ExtractContents(value)]; if (distance == 0f && block is CrossBlock && Vector3.Dot(direction, new Vector3(startCell) + new Vector3(0.5f) - start) < 0f) { return(false); } if (mode == RaycastMode.Digging) { return(!block.IsDiggingTransparent); } if (mode == RaycastMode.Interaction) { if (block.IsPlacementTransparent) { return(block.IsInteractive(m_subsystemTerrain, value)); } return(true); } if (mode == RaycastMode.Gathering) { return(block.IsGatherable); } } return(false); }); float num = bodyRaycastResult.HasValue ? bodyRaycastResult.Value.Distance : float.PositiveInfinity; float num2 = movingBlocksRaycastResult.HasValue ? movingBlocksRaycastResult.Value.Distance : float.PositiveInfinity; float num3 = terrainRaycastResult.HasValue ? terrainRaycastResult.Value.Distance : float.PositiveInfinity; if (num < num2 && num < num3) { return(bodyRaycastResult.Value); } if (num2 < num && num2 < num3) { return(movingBlocksRaycastResult.Value); } if (num3 < num && num3 < num2) { return(terrainRaycastResult.Value); } return(new Ray3(start, direction)); }
public T?Raycast <T>(Ray3 ray, RaycastMode mode, bool raycastTerrain = true, bool raycastBodies = true, bool raycastMovingBlocks = true) where T : struct { object obj = Raycast(ray, mode, raycastTerrain, raycastBodies, raycastMovingBlocks); if (!(obj is T)) { return(null); } return((T)obj); }
private void Start() { if (raycastMode == RaycastMode.Box && boxCollider == null) { boxCollider = gameObject.GetComponent <BoxCollider>(); if (boxCollider == null) { Debug.LogError($"Box raycast mode requires a BoxCollider, but none was found on {name}! Defaulting to Simple raycast mode."); raycastMode = RaycastMode.Simple; } } }
/// <summary> /// Validate all required components and sets event handlers. /// </summary> void Awake() { if (_overviewStatusText == null) { Debug.LogError("Error: RaycastExample._overviewStatusText is not set, disabling script."); enabled = false; return; } if (_raycastController == null) { Debug.LogError("Error: RaycastExample._raycastController is not set, disabling script."); enabled = false; return; } if (_raycastHead == null) { Debug.LogError("Error: RaycastExample._raycastHead is not set, disabling script."); enabled = false; return; } if (_raycastEyes == null) { Debug.LogError("Error: RaycastExample._raycastEyes is not set, disabling script."); enabled = false; return; } if (_controllerConnectionHandler == null) { Debug.LogError("Error: RaycastExample._controllerConnectionHandler not set, disabling script."); enabled = false; return; } _raycastController.gameObject.SetActive(false); _raycastHead.gameObject.SetActive(false); _raycastEyes.gameObject.SetActive(false); _raycastMode = RaycastMode.Controller; UpdateRaycastMode(); #if PLATFORM_LUMIN MLInput.OnControllerButtonDown += OnButtonDown; #endif }
private void FindCasters() { m_shadowCasters.Clear(); m_rayCount = 0; m_rayHitCount = 0; if ((m_raycastFrom & RaycastMode.TransformPosition) != 0) { RaycastFindShadowCaster(transform.position); } if ((m_raycastFrom & RaycastMode.BoundaryPoints) != 0) { foreach (var p in m_boundaryPoints) { if (p != null) { RaycastFindShadowCaster(p.position); } } } if ((m_raycastFrom & (RaycastMode.BoundsCenter | RaycastMode.BoundsCorners)) != 0) { if (m_boundsProvider == null) { Debug.LogWarning("Bounds provider not found. Disabled raycasting from bounds.", gameObject); m_raycastFrom &= ~(RaycastMode.BoundsCenter | RaycastMode.BoundsCorners); } else { if ((m_raycastFrom & RaycastMode.BoundsCenter) != 0) { RaycastFindShadowCaster(m_boundsProvider.worldCenter); } if ((m_raycastFrom & RaycastMode.BoundsCorners) != 0) { m_boundsProvider.GetWorldCorners(m_boundsWorldCorners); for (int i = 0; i < m_boundsWorldCorners.Length; i++) { RaycastFindShadowCaster(m_boundsWorldCorners[i]); } } } } }