/// <summary> /// Handles ray casting. /// </summary> private void HandleRayCasting() { HitItemChangeEvent changeEvent = new HitItemChangeEvent(); if (this.currentHitItemMetadata != null) { changeEvent.OldItem = new HitItemMetadata { ItemHit = this.currentHitItemMetadata.ItemHit, LayerHit = this.currentHitItemMetadata.LayerHit }; } // cycle through the layer priorities to see if one of them was hit by priority order foreach (Layer layer in layerPriorities) { RaycastHit?hitInfo = RaycastForLayer(layer); if (hitInfo != null && hitInfo.HasValue) { changeEvent.NewItem = new HitItemMetadata { ItemHit = hitInfo.Value, LayerHit = layer }; break; } } // update state to the new value this.currentHitItemMetadata = changeEvent.NewItem; // notify listeners if (OnHitItemChangeObservers != null) { OnHitItemChangeObservers(changeEvent); } }
public override bool Equals(object obj) { bool result = false; HitItemMetadata other = obj as HitItemMetadata; if (other != null) { result = this.LayerHit == other.LayerHit; result &= this.itemHit.collider != null && other.itemHit.collider != null; result &= this.itemHit.collider.Equals(other.itemHit.collider); } return(result); }
/// <summary> /// Handle the mouse click. /// </summary> private void HandleClickMovement(HitItemMetadata itemHit) { if (Input.GetMouseButton(0) && itemHit != null) { // print("Cursor raycast hit " + cameraRaycaster.hit.collider.gameObject.name.ToString()); = clickPoint = itemHit.ItemHit.point; switch (itemHit.LayerHit) { case Layer.Walkable: shortenedClickPoint = ShortenDestination(clickPoint, this.transform.position, this.walkStopRadius); break; case Layer.Enemy: shortenedClickPoint = ShortenDestination(clickPoint, this.transform.position, this.attackStopRadius); break; case Layer.RaycastEndStop: // do nothing break; default: print("you haven't handled the following layer yet " + itemHit.LayerHit); return; } } // move the player to the mouse click location, but only if within the correct radius of the item clicked Vector3 deltaVector = shortenedClickPoint - transform.position; // magnitudes are always positive and there is a floating arithmetic issue that causes magnitude to never hit 0 causing player to spin if (Mathf.MoveTowards(deltaVector.magnitude, 0, .2f) > 0) { this.MovePlayer(deltaVector); } else { this.MovePlayer(Vector3.zero); // don't move } }