@@ -587,68 +587,82 @@ public override void FinishBuildAimControls(bool isCancel)
587587 public void FindAndSetBuildingAreaByAxes ( Vector2 aimAxes )
588588 {
589589 Vector3 raycastPosition = CacheTransform . position + ( GameplayUtils . GetDirectionByAxes ( CacheGameplayCameraTransform , aimAxes . x , aimAxes . y ) * ConstructingBuildingEntity . BuildDistance ) ;
590- LoopSetBuildingArea ( physicFunctions . RaycastDown ( raycastPosition , CurrentGameInstance . GetBuildLayerMask ( ) ) , raycastPosition ) ;
590+ LoopSetBuildingArea ( physicFunctions . RaycastDown ( raycastPosition , CurrentGameInstance . GetBuildLayerMask ( ) ) ) ;
591591 }
592592
593593 public void FindAndSetBuildingAreaByMousePosition ( )
594594 {
595- Vector3 worldPosition2D ;
596- LoopSetBuildingArea ( physicFunctions . RaycastPickObjects ( CacheGameplayCamera , InputManager . MousePosition ( ) , CurrentGameInstance . GetBuildLayerMask ( ) , Vector3 . Distance ( CacheGameplayCameraTransform . position , MovementTransform . position ) + ConstructingBuildingEntity . BuildDistance , out worldPosition2D ) , worldPosition2D ) ;
595+ LoopSetBuildingArea ( physicFunctions . RaycastPickObjects ( CacheGameplayCamera , InputManager . MousePosition ( ) , CurrentGameInstance . GetBuildLayerMask ( ) , Vector3 . Distance ( CacheGameplayCameraTransform . position , MovementTransform . position ) + ConstructingBuildingEntity . BuildDistance , out _ ) ) ;
597596 }
598597
599598 /// <summary>
600599 /// Return true if found building area
601600 /// </summary>
602601 /// <param name="count"></param>
603- /// <param name="raycastPosition"></param>
604602 /// <returns></returns>
605- protected bool LoopSetBuildingArea ( int count , Vector3 raycastPosition )
603+ protected bool LoopSetBuildingArea ( int count )
606604 {
605+ ConstructingBuildingEntity . BuildingArea = null ;
607606 ConstructingBuildingEntity . HitSurface = false ;
608- IGameEntity gameEntity ;
607+ BuildingEntity buildingEntity ;
609608 BuildingArea buildingArea ;
610609 Transform tempTransform ;
610+ Bounds tempColliderBounds ;
611611 Vector3 tempVector3 ;
612612 for ( int tempCounter = 0 ; tempCounter < count ; ++ tempCounter )
613613 {
614614 tempTransform = physicFunctions . GetRaycastTransform ( tempCounter ) ;
615+ if ( ConstructingBuildingEntity . CacheTransform . root == tempTransform . root )
616+ {
617+ // Hit collider which is part of constructing building entity, skip it
618+ continue ;
619+ }
620+
615621 tempVector3 = physicFunctions . GetRaycastPoint ( tempCounter ) ;
616- if ( CurrentGameInstance . DimensionType == DimensionType . Dimension3D )
617- tempVector3 . y = physicFunctions . GetRaycastPoint ( tempCounter ) . y ;
622+ tempVector3 = GetBuildingPlacePosition ( tempVector3 ) ;
623+ tempColliderBounds = physicFunctions . GetRaycastColliderBounds ( tempCounter ) ;
618624
619- buildingArea = tempTransform . GetComponent < BuildingArea > ( ) ;
620- if ( buildingArea == null )
625+ if ( CurrentGameInstance . DimensionType == DimensionType . Dimension3D )
621626 {
622- gameEntity = tempTransform . GetComponent < IGameEntity > ( ) ;
623- if ( gameEntity == null || gameEntity . Entity != ConstructingBuildingEntity )
627+ // Find ground position from upper position
628+ bool hitAimmingObject = false ;
629+ Vector3 raycastOrigin = new Vector3 ( tempVector3 . x , tempColliderBounds . center . y + tempColliderBounds . extents . y + 0.01f , tempVector3 . z ) ;
630+ RaycastHit [ ] groundHits = Physics . RaycastAll ( raycastOrigin , Vector3 . down , tempColliderBounds . size . y + 0.01f , CurrentGameInstance . GetBuildLayerMask ( ) ) ;
631+ for ( int j = 0 ; j < groundHits . Length ; ++ j )
624632 {
625- // Hit something and it is not part of constructing building entity, assume that it is ground
626- ConstructingBuildingEntity . BuildingArea = null ;
627- ConstructingBuildingEntity . HitSurface = true ;
628- ConstructingBuildingEntity . Position = GetBuildingPlacePosition ( tempVector3 ) ;
629- break ;
633+ if ( groundHits [ j ] . transform == tempTransform )
634+ {
635+ tempVector3 = groundHits [ j ] . point ;
636+ ConstructingBuildingEntity . Position = tempVector3 ;
637+ hitAimmingObject = true ;
638+ break ;
639+ }
630640 }
631- continue ;
641+ if ( ! hitAimmingObject )
642+ continue ;
632643 }
633644
634- if ( buildingArea . IsPartOfBuildingEntity ( ConstructingBuildingEntity ) ||
635- ! ConstructingBuildingEntity . BuildingTypes . Contains ( buildingArea . buildingType ) )
645+ buildingEntity = tempTransform . root . GetComponent < BuildingEntity > ( ) ;
646+ buildingArea = tempTransform . GetComponent < BuildingArea > ( ) ;
647+ if ( ( buildingArea == null || ! ConstructingBuildingEntity . BuildingTypes . Contains ( buildingArea . buildingType ) )
648+ && buildingEntity == null )
649+ {
650+ // Hit surface which is not building area or building entity
651+ ConstructingBuildingEntity . BuildingArea = null ;
652+ ConstructingBuildingEntity . HitSurface = true ;
653+ break ;
654+ }
655+
656+ if ( buildingArea == null || ! ConstructingBuildingEntity . BuildingTypes . Contains ( buildingArea . buildingType ) )
636657 {
637658 // Skip because this area is not allowed to build the building that you are going to build
638659 continue ;
639660 }
640661
641662 ConstructingBuildingEntity . BuildingArea = buildingArea ;
642663 ConstructingBuildingEntity . HitSurface = true ;
643- ConstructingBuildingEntity . Position = GetBuildingPlacePosition ( tempVector3 ) ;
644664 return true ;
645665 }
646- if ( CurrentGameInstance . DimensionType == DimensionType . Dimension2D )
647- {
648- ConstructingBuildingEntity . BuildingArea = null ;
649- ConstructingBuildingEntity . HitSurface = false ;
650- ConstructingBuildingEntity . Position = GetBuildingPlacePosition ( raycastPosition ) ;
651- }
652666 return false ;
653667 }
654668
0 commit comments