/// <summary> /// Adjusts the throw by checking whether the XZ angle threshold is within the allowed range, and if so, adjust the rotation. /// </summary> /// <param name="thrownObject">The thrown <see cref="GazeThrowableObject"/>.</param> /// <param name="focusedGameObject">The focused GameObject.</param> /// <param name="rotatedVelocity">The rotated velocity of the thrown object.</param> /// <returns>True if the throw was within the angle threshold as set by the thrown object, otherwise false.</returns> private bool TryRotateThrow(GazeThrowableObject thrownObject, GameObject focusedGameObject, out Vector3 rotatedVelocity) { rotatedVelocity = Vector3.zero; // Get the velocity of the thrown object and project it on the XZ plane by zeroing the y component. var velocity = thrownObject.GetComponent <Rigidbody>().velocity; var velocityXz = velocity; velocityXz.y = 0; // Get the direction from the thrown object to the target and project on XZ plane. var correctDirectionXz = focusedGameObject.transform.position - thrownObject.transform.position; correctDirectionXz.y = 0; // Measure angle between the throw direction and the direction to the object. var angle = Vector3.Angle(velocityXz, correctDirectionXz); // If the angle is within the threshold set by the object, apply the rotation to the throw and return it as the rotatedVelocity variable. if (angle < thrownObject.XzAngleThresholdDegrees) { var side = AngleDir(velocityXz, correctDirectionXz, Vector3.up); rotatedVelocity = Quaternion.AngleAxis(angle * side, Vector3.up) * velocity; return(true); } return(false); }
/// <summary> /// Try to adjust the trajectory of a thrown object to hit a focused object by modifying the thrown object's trajectory with values set on the <see cref="GazeThrowableObject"/>. /// </summary> /// <param name="thrownObject">The <see cref="GazeThrowableObject"/> component of the thrown game object.</param> /// <param name="focusedGameObject">The focused game object.</param> /// <param name="initialVelocity">The initial velocity of the thrown game object.</param> /// <param name="throwVelocity">The resulting throw velocity.</param> /// <returns>True if the trajectory could be modified to hit the target.</returns> private bool TryAdjustTrajectory(GazeThrowableObject thrownObject, GameObject focusedGameObject, Vector3 initialVelocity, out Vector3 throwVelocity) { throwVelocity = Vector3.zero; // Get the bounds of the target object to be able to calculate a collision. var bounds = focusedGameObject.GetComponent <Collider>().bounds; // Call the interop to calculate the throw, passing in the position of the throw, bounds of target as well as the values configured in the GazeThrowableObject. var result = Interop.HEC_Calculate_Throw( thrownObject.transform.position, bounds.min, bounds.max, initialVelocity, _gravity, thrownObject.LowerVelocityMultiplier, thrownObject.UpperVelocityMultiplier, thrownObject.MaxYVelocityModifier, out throwVelocity ); // If the result of the method call was that there was no solution, return false, otherwise there is a solution. if (result == HEC_Error.NoSolution) { return(false); } return(true); }