Garry's Mod Wiki

Revision Difference

ENTITY:ResolveCustomFlyCollision#567694

<function name="ResolveCustomFlyCollision" parent="ENTITY" type="hook"> <added>2026.04.08</added> <description>Called during a non-VPhysics collision event for flying entities. This is best used to bounce off projectiles from surfaces. For this to be triggered, your entity must be the one that's colliding, have some velocity <page>ENTITY:SetMoveType</page> must either be `MOVETYPE_FLY` or `MOVETYPE_FLY_GRAVITY` and <page>ENTITY:SetMoveCollide</page> must be `MOVECOLLIDE_FLY_CUSTOM`. This is best used to make projectiles bounce off from surfaces in their own way. For this to be triggered, this entity must be the one that's colliding, have some velocity, <page>Entity:GetMoveType</page> must be either <page text="MOVETYPE_FLY">Enums/MOVETYPE#MOVETYPE_FLY</page> or <page text="MOVETYPE_FLYGRAVITY">Enums/MOVETYPE#MOVETYPE_FLYGRAVITY</page>, and <page>Entity:GetMoveCollide</page> must be <page text="MOVECOLLIDE_FLY_CUSTOM">Enums/MOVECOLLIDE#MOVECOLLIDE_FLY_CUSTOM</page>. <note>This works only on `anim` type entities.</note></description> <realm>Server</realm> <args> <arg name="traceResult" type="table{TraceResult}">The <page>Structures/TraceResult</page> where the collision occured.</arg> <arg name="vel" type="vector">The calculated velocity after calculations such as bounciness, elasticity, ground sliding etc...</arg> </args> <rets> <ret type="boolean">Return `true` to prevent default action.</ret> </rets> </function> <example> <description>A rocket model which directly reflects off from surfaces without any velocity loss, similar to `sent_ball`.</description> <code> AddCSLuaFile() ENT.Base = "base_anim" ⤶ ENT.Type = "anim" ⤶ ⤶ ENT.Base = "base_anim"⤶ ENT.Type = "anim"⤶ ENT.PrintName = "Bouncy Rocket" ENT.Category = "Other" ENT.Editable = true ENT.Spawnable = true ENT.AdminOnly = true ENT.RenderGroup = RENDERGROUP_TRANSLUCENT ⤶ ENT.PhysicsSolidMask = MASK_SOLID + CONTENTS_HITBOX -- collide with vphysics ⤶ function ENT:Initialize() self:SetModel("models/weapons/w_missile_closed.mdl") self:SetMoveType(MOVETYPE_FLY) self:SetMoveCollide(MOVECOLLIDE_FLY_CUSTOM) self:SetCollisionGroup(COLLISION_GROUP_PROJECTILE) ⤶ self:SetSolid(SOLID_BBOX) -- turns on collision with other entities self:SetCollisionBounds(Vector(-1,-1,-1),Vector(1,1,1)) self:SetVelocity(self:GetForward()*650) ⤶ end ⤶ ⤶ function ENT:ResolveCustomFlyCollision(traceResult, vel)⤶ ⤶ vel = self:GetVelocity() -- use unmodified velocity ⤶ local normal = vel - 2 * vel:Dot(traceResult.HitNormal) * traceResult.HitNormal⤶ ENT.RenderGroup = RENDERGROUP_TRANSLUCENT⤶ ENT.PhysicsSolidMask = MASK_SOLID + CONTENTS_HITBOX -- collide with vphysics⤶ function ENT:Initialize() ⤶ self:SetModel( "models/weapons/w_missile_closed.mdl" ) self:SetMoveType( MOVETYPE_FLY ) self:SetMoveCollide( MOVECOLLIDE_FLY_CUSTOM ) self:SetCollisionGroup( COLLISION_GROUP_PROJECTILE ) self:SetSolid( SOLID_BBOX ) -- turns on collision with other entities⤶ self:SetCollisionBounds( Vector( -1, -1, -1 ), Vector( 1, 1, 1 ) ) self:SetVelocity( self:GetForward() * 650 ) ⤶ end⤶ ⤶ function ENT:ResolveCustomFlyCollision( traceResult, vel ) ⤶ vel = self:GetVelocity() -- using unmodified velocity⤶ local speed = vel:Length() normal = normal:GetNormalized() ⤶ self:SetLocalVelocity(normal * speed) self:SetAngles(normal:Angle()) ⤶ end⤶ ⤶ local tempdir = traceResult.HitNormal⤶ tempdir:Mul( 2 * vel:Dot( tempdir ) ) ⤶ local dir = vel⤶ dir:Sub( tempdir ) dir:Normalize()⤶ ⤶ local reflectionangle = dir:Angle()⤶ ⤶ local newvel = dir⤶ newvel:Mul( speed )⤶ self:SetLocalVelocity( newvel )⤶ ⤶ self:SetAngles( reflectionangle )⤶ ⤶ end⤶ </code> </example>