/// <summary> /// Find a Platform from a given position and direction /// used for the ball to know if its running into a platform, or if /// rolling off an edge /// </summary> /// <param name="pos"></param> /// <param name="dir"></param> /// <returns></returns> Platform RaycastForPlatform(Vector3 pos, EDirection dir) { return(Physics.RaycastAll(pos, dir.ToVec(), 1.0f) .Select(o => o.collider.GetComponent <Platform>()) .Where(o => o != null) .FirstOrDefault()); }
private void GetPivotOffsetAndAxis(EDirection dir, out Vector3 pivot, out Vector3 axis) { pivot = Vector3.zero; axis = Vector3.zero; Vector3 dirVec = dir.ToVec(); switch (dir) { case EDirection.FORWARD: pivot = new Vector3(0, -0.5f, 0.5f); break; case EDirection.BACKWARD: pivot = new Vector3(0, -0.5f, -0.5f); break; case EDirection.LEFT: pivot = new Vector3(-0.5f, -0.5f, 0); break; case EDirection.RIGHT: pivot = new Vector3(0.5f, -0.5f, 0); break; } axis = Vector3.Cross(Vector3.up, dirVec); Vector3 scale = GetRotatedScale(target.transform.rotation); pivot.x *= scale.x; pivot.y *= scale.y; pivot.z *= scale.z; }
void UpdateRollRotation(Vector3 targetDirVec) { // make sure the requested roll direction is normalised targetDirVec.Normalize(); // get the target direction we want to move in. EDirection targetDirection = targetDirVec.ToDir(); targetDirVec = targetDirection.ToVec(); Vector3 currentDirectionVec = rollDirection.ToVec(); // if we are trying to move in a direction perpendicular to the current rolling direction... dont // this will mean the rolling will continue as if no input is pressed float requestIsPerpendicularToCurrentDir = Vector3.Dot(targetDirVec, currentDirectionVec); if (Mathf.Abs(requestIsPerpendicularToCurrentDir) < 0.25f && rollDirection != EDirection.NONE) { targetDirVec = Vector3.zero; targetDirection = EDirection.NONE; } // are we beginning to attempt a roll... // check if anything is in the way if (rollDirection == EDirection.NONE && targetDirection != EDirection.NONE) { // check if we can move in the desired direction if (IsBlockedFromMovementInDirection(targetDirection)) { targetDirVec = Vector3.zero; targetDirection = EDirection.NONE; } } var autoCompleteRoll = props.autoCompleteRoll; var completeRollThreshold = props.completeRollThreshold; var rollSpeed = props.rollSpeed; // if targetDirection is none, continue rotation until rolling completes if (autoCompleteRoll && targetDirection == EDirection.NONE && rollDirection != EDirection.NONE) { if (rollRotation < completeRollThreshold) { rollRotation -= 90 * Time.deltaTime * rollSpeed; } else { rollRotation += 90 * Time.deltaTime * rollSpeed; } } else if (rollDirection == EDirection.NONE || targetDirection == rollDirection) { rollDirection = targetDirection; rollRotation += Mathf.Abs(targetDirVec.x) * Time.deltaTime * 90.0f * rollSpeed; rollRotation += Mathf.Abs(targetDirVec.z) * Time.deltaTime * 90.0f * rollSpeed; } else { rollRotation += -Mathf.Abs(targetDirVec.x) * Time.deltaTime * 90.0f * rollSpeed; rollRotation += -Mathf.Abs(targetDirVec.z) * Time.deltaTime * 90.0f * rollSpeed; } rollRotation = Mathf.Clamp(rollRotation, 0, 90); }
public static EDirection GetInverseDir(this EDirection dir) { return((-dir.ToVec()).ToDir()); }