UE4 Objects overlap position after collision - unreal-engine4

I'm new to unreal engine, I'm trying to add large force to an object with a box collider but after it collide with other object (just another instance) the overlap inside each others and become like one object and moving with each others.
Cab anyone explain their behavior and how i should resolve this?

What happens here is that both objects collide with each other continously. To fix that you could try to deactive the OnOverlap()-Event on either the overlapping Object or the colliding object.
In blueprints you can achieve that by setting the Generate Overlap Events-Variable of one of the colliding static meshes of the overlapping objects to false.
In C++ you could simply remove the dynamic event callback for one of the colliding objects like that:
CollidingComponent->OnComponentBeginOverlap.RemoveDynamic(this, &ACollidingActor::OnBeginOverlap);
Where CollidingComponent is the component of your object, which causes the overlap event to trigger.

Like #Alex said, they overlap with each other, over and over. If you didn't know, you can add breakpoints to your blueprint nodes, just like in your code, by right-clicking a node and select Enable Breakpoint (or smth like that). Your game will stop when reaching it and switch to that exact point in your blueprint. You can then hover over that node and see every variables value going in and out of it.
Hope this helps you learing to use the Unreal Engine.

Related

Is there a way to get an information who started a collision?

I have two actors in my project, sometimes AI Pawn is causing a collision and sometimes a Player is causing it. Is there a way to distinguish it?
Have you checked the onHit function for the AI Pawn and Player?
Both in C++ and Blueprint, collision events give you a FHitResult (displayed as just HitResult in BP). Here is reference.
This result contains both the Actor and Component that was hit/overlapped/traced.
So, the object calling the collision event is first object, and YourFHitResult.Actor is the other object.
In BP, you can use the break hit result node to get the actor/component.

Unreal Engine 4 - which class spawned the shot?

In Unreal Engine 4 I have two different classes (Player and Enemy) that spawn the same type of Actor (Shot). From within the newly created Shot, how can I find out which of those two classes spawned it?
I am sure there is a pretty straightforward answer to it, but I can't seem to phrase it properly when searching, as I am not getting any helpful hits.
Note: I have started digging into UE4 very recently, so it is quite possible that I have found the answer already but am just not sure what I am looking for. Or that I should simply have two different classes (Player Shot and Enemy Shot). Any help appreciated!
Edit: GetInstigator() gets me the values I expected but the description gives me the impression this is not necessarily what I am looking for: The APawn that is responsible for damage done by the spawned Actor. (Can be left as NULL).
If you're using BP, in the Shot class, create a variable (actor reference). When you spawn the actor, cast to the Shot reference, using the Return Value from the SpawnActor node as the object. After making this cast, SET the actor reference variable you created to the Self reference of what pawn spawned it.
Basically, what you are doing here is when the Shot is spawned, a reference to the pawn (player or enemy) is created within it. Hope this helps!

Why is "Cast to BP_Ladder" failing all the time?

I am having trouble with my Unreal Engine 4 project. I'm very new to this and i don't really understand what's going on. I have made ladder functionality so the character can climb up the ladder and stand on the Static Mesh that is on top. But when i want to go down the ladder i want to trigger the "Allow Down" function on Actor "BP_Ladder" but the cast is failing everytime. what is causing the casts to fail?
I've looked around and other people with cast failed problems have mostly had the names wrong but my ladder is called "BP_Ladder" and that is what i'm casting to so that leaves me really confused.
My BP_Dude blueprint (this is being called every tick)
My BP_Ladder blueprint (this is what i'm trying to trigger)
The goal for this function is that the collision of the static mesh will turn off and then allow me to move down the ladder like normal.
I'd really appreciate your help with this, its my first couple of days using unreal engine and the Epic Games tutorials i followed didn't show all of the blueprints so everyone was left helpless with half broken blueprints.
Understanding Casts
In its simplest form, if your Actor can be cast to BP_Ladder, then it will return the casted object on the output pin.
Just to ensure you know how a cast works, I have to point out that you can't cast a given type to some unrelated arbitrary type; it has to be "castable" to the cast destination. So if you think that the Actor object returned by your overlap is genuinely a BP_Ladder, or a blueprint class that derives from BP_Ladder, then you should be OK. But that has to be the case; otherwise it will fail every time.
https://docs.unrealengine.com/en-us/Engine/Blueprints/UserGuide/CastNodes
Sorry if I'm being patronising, but if a cast isn't working 9/10 it hasn't been used correctly.
Debugging
OK that out of the way, if you think you are genuinely casting to the correct type and it's still failing, then you'll need to debug your blueprint with the objective of finding what type is being given to the cast node which results in the failure.
Select the cast object in your blueprint.
Press F9 to create a breakpoint; a red circle should appear on the top left of your cast box
Run your game in PIE mode (Combo next to play, New Editor Window (PIE))
Put the game in a scenario where your cast will fire (I presume touch the ladder)
The breakpoint should trigger; your game window will go grey and you'll get a large red arrow pointing down towards your cast box where you placed the breakpoint
Hover over the Object pin on the input side of the cast box. It should show a alt-text containing details of the variable.
Inside the alt-text box, look for Current value; this should show you the current object type that you are about to cast. You need to ensure that this value is what you expect.
https://docs.unrealengine.com/en-US/Engine/Blueprints/UserGuide/Debugging
Example
I've taken a screenshot of a working game; in this you will see:
a Breakpoint (the red circle) is shown on the function node (the box)
the Execution node (red arrow) is Create Rolling Stock
the Current value of the variable is DepotComponent...Depot
Final thoughts
There's a couple of gotchas when using blueprints; one of them in this case could be that you can have two classes with the same name (although they might have different internal "script names" - effectively a fully qualified path). When you debugging an object variable, make sure you match the entire path to the location of your asset in your content folder; this will ensure that you are indeed attempting to cast to the object you think you really are.
Another classic gotcha is "hot reloads". Sometimes the editor needs to reload a module after an on-the-fly build but a class conflict occurs internally; this results in the new version of the class getting a different name (prefixed with HOTRELOADED). Watch out for this; it can cause you to be dealing with two distinct types when casting and you don't even realise. A real sanity-sapper. If in doubt, close and re-open the editor; it fixes it every time.
On Begin Overlap is only triggered when you START an overlap, so if you have some logic that sets your "can climb" to false you will need to move outside of the collider and back into it again for another Begin Overlap event.

Transparent layer Unity

In my scene I have 2 camera(split screen). it's possible change the trasparency of a layer only for one camera? for examples the "layer1" have alphatrasparency = 0.5 for right camera while left camera show "layer1" without trasparency.
Ostensibly no
There's a way to do it, though. It's a bit of a hack though, as it doesn't depend on physics layers, but rather the presence of a custom monobehavior script. Here's what I remember about this off the top of my head (I can dig up an implementation later, if needed).
Step 1: you will need a MonoBehaviour script attached to every game object you want to have rendered differently. This script is absolutely essential.
Step 2: this script will contain one function (you can use an existing behaviour script if all the objects already have one in common and you can just add this function to that script). Call it whatever you want, but something like AboutToBeRendered(Camera cam)
Step 3: create another script and attach it to both cameras. This script will also have one function in it: OnPreRender()
Step 4: In this OnPreRender method you will need to do:
find all game objects from Step 1
get their component with the AboutToBeRendered method
invoke that method, passing the camera as the paramter
Step 5: Writing the AboutToBeRendered method.
determinie which of the two cameras was passed to the method
set the material's color to be transparent or not, as needed
OnPreRender() is only called on scripts with a camera component on the same GameObject, indicating that this camera is about to render the scene. But what we actually want is for the object about to be rendered to know that it's about to be rendered and by which camera. Which is why we need the script in step 1.
I suppose you could skip step 1 and only look at every object in the scene and look at its physics layer, but that's going to be more expensive to figure out than "get me all instances of this component." You could do it based on Tag instead, as FindObjectsWithTag is generally considered to be pretty fast, but if you're already using the tag for something else, you're out of luck, and there's no comparable method for getting objects in a given physics layer.
However, you'd have to tweak the material's alpha value in the camera script rather than letting the object itself decide what value it should be.
In either case, the object's material would need to support transparency. When I did this I was preventing the object from being rendered entirely, so I just disabled its MeshRenderer component.

Unity - Selective Collisions - Again

I know this subject has been discussed here a bunch of times already. But I'm stuck here.
I am developing a simple game where I have a few rigid bodies piled and a projectile is intended to hit those gameObjects.
But some of those rigid bodies are collectibles, elements that give points if hit with the projectile.
My question is: I need those collectibles to behave just like the other rigid bodies, but as a trigger for the projectile.
How can I do it?
Regards.
I'm not one hundred percent sure I understand what you want, but here is what I think you need to be doing.
Set a tag on the collectible called "Collectible"
And then in the code write the following
Void OnCollisionEnter(Collision other){
if(other.tag == "Collectible"){
CollectCollectible();
}
}
OnCollisionEnter is run whenever something collides with the object, checking a tag is far from the most effecient way of doing this, but it's probably the easiest.
I'm not sure if I understand you right. Attach a Script to the collectibles and let it implement OnCollisionEnter. If you don't know which ones are the collectibles at design time, you can do it at runtime via AddComponent <MyCollectibleScript> ().
Another (pretty dirty) way is to take different physic materials but this just for the sake of completeness.