Hello @JazzJack3D
To begin with, we will create the two collision channels we need, which we will use later:
The names can be anything you like; what’s important is setting the Default Response correctly.
Next, we will prepare the cube itself that we will be lifting.
The logic here is that the cube itself will be a physical body, subject to falling and other forces, while the collision box, which is its child, will generate overlap events and store all objects it overlaps with. This will be useful later on. The collision box stores them by default, so we won’t need to implement this ourselves.
Let’s set up the collisions:
For the cube, we need two channels—specifically, the ones we created: IntCubeTrace
and IntCubeBox
.
For the cube, we should set both of these channels to Ignore
.
Also, don’t forget to enable Simulate Physics for the cube.
For the box, it is important to set IntCubeTrace
to Block
—this will be useful later. As for IntCubeBox
, set it to Overlap
—this will allow the cube to overlap with other cubes.
To allow the cube to overlap with the environment, set Overlap
to the channels associated with the desired environment elements(“Object Type” parameter), typically World Static
and World Dynamic
.
It is also important to check the box for Generate Overlap Events .
To ensure that the box overlaps with environment elements, we need to make sure that the IntCubeBox
channel of the desired element is set to Overlap
(this should be the default setting, as we set the standard value for the IntCubeBox
channel to Overlap
).
Additionally, we need to check the box for Generate Overlap Events and ensure that the channel specified in the Object Type
is set to Overlap
in the box’s collision settings.
Now that we have set everything up, we can proceed with creating the logic in our character (I will work in the character’s event graph, where the camera rotation settings and other settings are located).
Here is everything we need to do; let’s break it down step by step:
This set of nodes will create a ray. The start of the ray is the world position of the camera, and the end of the ray is a point directly in front of the camera at a distance of 500 cm.
We choose IntCubeTrace
as the channel, which we created earlier. As we recall, this channel overlaps only with the collision box of our cube, meaning the ray can only interact with it.
This node will return the actor the ray interacted with, which is the cube we are looking at. We’ll discuss the branch later.
Immediately after the ray returns the desired object, we perform a cast to our cube, allowing us to access its parameters and components. The cast also helps filter out irrelevant ray returns (such as if we are not looking at a cube or if the cube is beyond 500 cm).
Next, we save the cube actor in a variable of type IntCube Object Reference
. We also retrieve the static mesh cube from the actor and save it in a variable of type StaticMesh Component Object Reference
.
Then, we call the PickUpCube
event, which we will discuss later.
This is the implementation of the PickUpCube
event. This event attaches the cube to the camera and detaches it, while also toggling the physics simulation and collision for the lifted cube using the Change Cube Collision
event (we will discuss this event later).
Additionally, the PickUpCube
event manages the attached
variable, which indicates whether the cube is lifted or not. This variable is used in the branch at the beginning.
Depending on the value of the pickup
input, the event will either attach or detach the cube.
The ChangeCubeCollision
event toggles the physics simulation and collision for the cube, depending on the value of the input Simulate
.
When we pick up the cube, its collision and physics simulation are disabled, but the collision of the box continues to work. This allows the cube to move inside other objects while still being able to determine what our cube is overlapping with, using the collision of the box.
We just need to understand what happens if the attached
variable results in true
at the very beginning and the logic flows upward.
Here, we determine how many objects our cube is currently overlapping with. As we remember, we configured the collision channels for the box and environment elements, and we are using the result of those settings here.
We obtain an array of all the objects that the box has overlapped with and determine their quantity by checking the length of the array. If the length is greater than 0, it means the box is intersecting with something, so we don’t release the object and call the necessary logic to show an error, in my case, a print string. But if the length of the array is zero, it means the box is not intersecting with anything, and we can release it by calling PickUpCube
and setting the input pickup
to false
.
This will help you understand collisions, how to set them up, and create logic for them. It will be very useful for working in Unreal Engine.