void Update() { // Update head position and view direction _headPosition = Camera.main.transform.position; _viewDirection = Camera.main.transform.forward; // If we're simply looking around, reposition cursor if (_gazing) { RaycastHit hitInfo; // Check if we hit any meshes in the view direction if (Physics.Raycast(_headPosition, _viewDirection, out hitInfo)) { // Turn on renderer for cursor _cursorRenderer.enabled = true; // Set cursor at hit position and rotate it so that the up-axis points along normal _cursorTransform.position = hitInfo.point; _cursorTransform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal); // Set the hit object as the currently focused object _focusedObject = hitInfo.collider.gameObject; // Special check for if the object is the cannon, since the colliders are on the base and the barrel and we want to target the cannon as a whole if (_focusedObject.CompareTag("CannonChild")) { _focusedObject = _focusedObject.GetComponentInParent <CannonController>().gameObject; } else if (_focusedObject.CompareTag("Target")) { _focusedObject = _focusedObject.transform.parent.gameObject; } } else { // Don't render the cursor and set focused object to null _cursorRenderer.enabled = false; _focusedObject = null; } } else { // Don't render the cursor when we aren't trying to target something _cursorRenderer.enabled = false; } if (_moving) { _focusedObject.transform.position += _moveDir * _moveSpeed; } if (_rotating) { if (_focusedObject.CompareTag("Cannon")) { _pitch = _cannonController.GetPitch() + (_pitchPace * _rotationSpeed * Time.deltaTime); _yaw = _cannonController.GetYaw() + (_yawPace * _rotationSpeed * Time.deltaTime); _cannonController.Aim(_pitch, _yaw); } else { Transform targetContainer = _focusedObject.transform; targetContainer.eulerAngles = new Vector3(0.0f, targetContainer.eulerAngles.y + _yawPace * Time.deltaTime, 0.0f); } } }