HitComponent

Sets the entity’s collision area and provides an interface for hit reactions when receiving an attack from AttackComponent.

# Properties

[{"index":"Vector2 BoxOffset (Deprecated) (Sync)","0":"Available on previous systems where IsLegacy is true. Sets the center point position of the collider rectangle based on Entity."}]

[{"index":"Vector2 BoxSize (Sync)","0":"Specifies the width and height of the rectangular collider."}]

[{"index":"float CircleRadius (Sync)","0":"The radius of the circular collider. Valid when ColliderType is Circle."}]

[{"index":"string ColliderName (Deprecated) (ReadOnly) (HideFromInspector)","0":"This property is deprecated. Use CollisionGroup."}]

[{"index":"Vector2 ColliderOffset (Sync)","0":"Sets the center point position of the collider based on Entity. Available on new systems with IsLegacy set as false."}]

[{"index":"ColliderType ColliderType (Sync)","0":"Sets the type of collider. Available on new systems with IsLegacy set as false."}]

[{"index":"CollisionGroup CollisionGroup (Sync)","0":"Set up a collision group."}]

[{"index":"boolean IsLegacy (ReadOnly)","0":"Whether or not this Component works with the old system. The new system allows the Collider to be subject to the TransformComponent's rotation and size. You can also use a circle-shaped collider by setting the ColliderType."}]

[{"index":"SyncList PolygonPoints (Sync)","0":"The positions of the points that make up the polygonal collider. Valid when ColliderType is Polygon."}]

##### inherited from Component:

[{"index":"boolean Enable (Sync)","0":"Checks whether Component is activated or not."}]

[{"index":"boolean EnableInHierarchy (ReadOnly) (HideFromInspector)","0":"Displays whether this Component is Enable in the hierarchy. If Entity Enable is false regardless of Component Enable, returns false."}]

[{"index":"Entity Entity (ReadOnly)","0":"The Entity that owns this component."}]

# Methods

[{"index":"boolean IsHitTarget(string attackInfo) (ScriptOverridable)","0":"Determines whether an Entity is subject to AttackComponent's attack. The default behavior is true.attackInfo is custom data set by the creator that was passed from AttackComponent."}]

[{"index":"void OnHit(Entity attacker, integer damage, boolean isCritical, string attackInfo, int32 hitCount) (ScriptOverridable)","0":"Called when an Entity is hit. The default action is to raise a HitEvent.attacker is the Entity that initiated the attack, attackInfo is the custom data passed from AttackComponent, and hitCount is the number of times the damage split is played."}]

##### inherited from Component:

[{"index":"boolean IsClient()","0":"Returns whether the current execution environment is a client or not."}]

[{"index":"boolean IsServer()","0":"Returns whether the current execution environment is a server or not."}]

# Events

[{"index":"HitEvent","0":"This event occurs when an entity is hit."}]

# Examples

This is an example of increasing the BoxSize of `HitComponent` in inverse proportion to remaining health whenever a monster is attacked by a player.
    
    
    @Component
    script VulnerabilityScaler extends Component
        @Sync
        property number Health = 1000
        property number InitialHealth = 0
        property Vector2 InitialBoxSize = Vector2(0,0)
    
        @ExecSpace("ServerOnly")
        method void OnBeginPlay()
        self.InitialHealth = self.Health
        self.InitialBoxSize = self.Entity.HitComponent.BoxSize:Clone()
        end
    
        @ExecSpace("ServerOnly")
        @EventSender("Self")
        handler HandleHitEvent(HitEvent event)
        -- Parameters
        local AttackCenter = event.AttackCenter
        local AttackerEntity = event.AttackerEntity
        local Damages = event.Damages
        local Extra = event.Extra
        local FeedbackAction = event.FeedbackAction
        local IsCritical = event.IsCritical
        local TotalDamage = event.TotalDamage
        --------------------------------------------------------
    
        local hitComponent = self.Entity.HitComponent
    
        self.Health = self.Health - TotalDamage
        self.Health = math.max(self.Health, 0.0)
    
        if self.Health > 0.0 then
        	local ratio = 10 - ((10 - 1) / self.InitialHealth) * self.Health
        	hitComponent.BoxSize = self.InitialBoxSize * ratio
        else
        	_EntityService:Destroy(self.Entity)
        end
        end
    end
    

`HitComponent` can be extended to directly implement both hit detection and actions to take when hit. The following is the IsHitTarget function of the PlayerHit script, which is a built-in script. This makes the player invincible for 1 second when hit.
    
    
    @Component
    script ImmunityManager extends Component
        property number ImmuneCooldown = 1
        property number LastHitTime = 0
    
        override method boolean IsHitTarget()
        local currentTime = _UtilLogic.ElapsedSeconds
        	if self.LastHitTime + self.ImmuneCooldown < currentTime then
        	self.LastHitTime = _UtilLogic.ElapsedSeconds
        return true
        end
    
        return false
        end
    end
    

# SeeAlso

  * AttackComponent
  * UtilLogic
  * EntityService
  * [Attack and Hit](/docs?postId=206)