/// <summary> /// Returns the planet at a given position if it exists. Null otherwise. /// </summary> /// <param name="position">The position to look for a planet</param> /// <returns>The planet at given position</returns> public static Planet GetPlanetAtPosition(Vector3 position) { RaycastHit hitInfo; Ray rayhit = Camera.main.ScreenPointToRay(position); // Raycast only on PLANET layer and ignore triggers Physics.Raycast(rayhit, out hitInfo, 1000, 1 << LayerManager.ToInt(LAYER.PLANET), QueryTriggerInteraction.Ignore); if (hitInfo.collider != null) { return(hitInfo.collider.GetComponent <Planet>()); } return(null); }
/// <summary> /// Returns the world position of the mouse along the (0,0,0) plane /// </summary> /// <returns>The mouse position in the world</returns> public static Vector3 GetMousePositionInWorld() { RaycastHit hitInfo; Ray rayhit = Camera.main.ScreenPointToRay(Input.mousePosition); // Raycast only on the FLOOR layer and ignore triggers Physics.Raycast(rayhit, out hitInfo, 1000, 1 << LayerManager.ToInt(LAYER.FLOOR), QueryTriggerInteraction.Ignore); if (hitInfo.collider != null) { return(hitInfo.point); } return(Vector3.zero); }
/// <summary> /// Draws a prediction of the planet's trajectory /// </summary> private void DrawPrediction() { float timeStep = PredictionTime / PredictionSteps; LineRenderer lr = GetComponent <LineRenderer>(); if (lr == null) { return; } lr.numPositions = PredictionSteps; // First point is current position lr.SetPosition(0, transform.position); // These will hold last prediction's values Vector3 lastVelocity = Velocity; Vector3 lastPosition = transform.position; for (int i = 1; i < PredictionSteps; i++) { // Get all the forces Vector3 forcesThisStep = GetSumForcesApplyingToMe(lastPosition); // Calculate velocity at given time Vector3 velocityThisStep = lastVelocity + forcesThisStep * timeStep * i; // (timeStep * i) is the equivalent of Time.deltaTime in the prediction // Deduce positin Vector3 positionThisStep = lastPosition + velocityThisStep * timeStep * i; lr.SetPosition(i, positionThisStep); // Raycast from last point to new point to check if the prediction is crossing a planet RaycastHit hitInfo; Ray rayhit = new Ray(lastPosition, positionThisStep - lastPosition); Physics.Raycast(rayhit, out hitInfo, Vector3.Distance(positionThisStep, lastPosition) * 2f, 1 << LayerManager.ToInt(LAYER.PLANET), QueryTriggerInteraction.Ignore); // If we hit something stop drawing the prediction if (hitInfo.collider != null) { lr.numPositions = i + 1; i = PredictionSteps - 1; } lastPosition = positionThisStep; lastVelocity = velocityThisStep; } }