In the game development often uses the ray collision, for instance the laser shot, needs to know clearly its centralized position, then releases the attack effect at this point.
Unrea Engine 4 To do Ray collision is also very simple, the main function of the implementation is the world Linetracesingle this method, the following gives the test code. The code I tested in Thirdpersontemplate. Detects the actor in front of the character with or without a bump, and displays a debug ball on the collision point. The code is as follows:
void Ananprojectcharacter::raycast () {Fhitresult hitresult (forceinit); Fvector Pos, dir; Fcollisionqueryparams CCQ (FName (TEXT ("Combattrace")), true, NULL); Ccq.btracecomplex = True;ccq.btraceasyncscene = False;ccq.breturnphysicalmaterial = False;ccq. Addignoredactor (this);p OS = getactorlocation (); const Frotator Rotation = Capsulecomponent->getcomponentrotation () ; Const Frotator yawrotation (0, Rotation.yaw, 0);//Get forward Vectordir = Frotationmatrix (yawrotation). Getunitaxis (EAXIS::X); Fvector posbegin = pos; Fvector posend = pos + dir * 500; Getworld ()->linetracesingle (HITRESULT,POSBEGIN,POSEND,ECC_WORLDSTATIC,CCQ);D Rawdebugline (This->GetWorld ( ), Posbegin, Posend, Fcolor (1.0f, 0.F, 0.F, 1.f), False, 20.F), if (Hitresult.getactor ()) {Drawdebugsphere (Getworld (), Hitresult.location, ten, fcolor::red, false, 20.F);}}
Note that you need to exclude the character from the collision detection itself. The final effect is as follows:
Unreal Engine 4 C + + Ray collision