Hello! Very simple question. I’d like to open a door by pressing a button. Can this be done by using raycasting to determine when the character comes into contact with the door static mesh and then use lerp on the z axis for the door? That is what I am thinking but I am not sure if raycasting allows you to specify a specific static mesh i.e. a door.
Ray trace to find what the character is looking at or interacting with is pretty common. The exact implementation will vary by game mechanics.
Here’s a snippet of our trace code to find what actor/component player reticle is above when walking around. It is executed on Tick() in the player controller to determine stuff like object highlighting, clicking objects, mouse off, mouse hover, etc. Should get you started:
// cannot ray trace without player controller
if (_playerController == nullptr)
{
return;
}
// get current camera location and rotation
FVector cameraLocation = _playerController->PlayerCameraManager->GetCameraLocation();
FRotator cameraRotation = _playerController->PlayerCameraManager->GetCameraRotation();
FVector cameraDirection = cameraRotation.Vector().GetSafeNormal();
// calculate ray start point, ray end point, hit result, collision parameters
// ray start point is the camera location
// ray end point is a ray cast through the middle of the screen
// collision query parameters determined by player controller (mainly to ignore hitting certain objects)
FVector traceStartLocation = cameraLocation;
FVector traceEndLocation = cameraLocation + MAXIMUM_INTERACTION_DISTANCE * cameraDirection;
FHitResult traceHitResult(ForceInit);
FCollisionQueryParams traceParameters = _playerController->GetCollisionQueryParameters();
// do ray trace
_playerController->GetWorld()->LineTraceSingleByChannel(traceHitResult, // result
traceStartLocation, // start
traceEndLocation, // end
ECC_Pawn, // collision channel
traceParameters); // trace parameters
// get unreal trace actor and component
_traceActor = traceHitResult.GetActor();
_traceComponent = traceHitResult.GetComponent();
// you now have the actor and the component hit by raytrace
// do something ...
Thank you I will work with this!