Unity - Selective Collisions - Again - unity3d

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.

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.

Unity 3D: what is the correct way to detect that the collision has already been processed for another collision participant?

I have a class CollisionHandler which has the OnCollisionEnter method. Each collideable entity in the game has CollisionHandler as a component. So, when 2 objects collide the OnCollisionEnter method is called twice and it's ok because the damage and other things are processed as a result of touching object "B" by object "A" and vice versa.
In addition, each collision creates a flash effect and plays a sound of impact and of course, these effects are also played twice for each collision. Although it wouldn't be imperceptible to the player it doesn't seem to be correct anyway. In order to prevent it, I came up with the following solution: I save the current frame number in another collision participant so it will know that these effects have been played already by the first one.
private void OnCollisionEnter(Collision collision)
{
// Calculate the damage caused by `this.gameObject` to `collision.gameObject`.
// ...
// Play an impact sound and show a visual effect.
if (thisObject.CollisionFrameId != Time.frameCount)
{
otherObject.CollisionFrameId = Time.frameCount;
// play sound
// show hit effect
}
}
Although it works I'm not sure (I'm a novice in Unity) that it's the best practice and if so please suggest other possible solutions.
Note that I didn't ask "Why is OnCollisionEnter getting called twice?". I know why it happens. My question is about the other, though it does mention the name of the same function it still doesn't asks the same.
You could do a GetComponent on the other object with one of your scripts (or the same script) and set the RecentCollisionObject to the current script or gameObject (and maybe a given time frame just for additional safety (if only one triggers it, but not the other for some reason)).
On each collision you check if RecentCollisionObject is the other object (and if no more than ~0.01 seconds has passed). If true, you know that particular collision must already have been processed by the other object just now, so reset all values from that other object and do nothing else. So the next collision which occurs, no matter how soon, will do this cycle anew and work as expected.
This way you let only the first to trigger the collide code execute your code (like a sound). The point is that both collision events are ran in the same frame, so no matter which triggers first, you can prevent the second from running.
You can use the Unity Tag System, you tag action to occur when colliding with that specific tagged object.
As example:
internal void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Oject Tag") {
this.GetComponent<AudioSource> ().PlayOneShot (objectTagSoundClip);
}
Just need a audio to play placed in game object.
Maybe you have 2 Colliders. If so you could check if boxcollider for example is colliding only. You could also try on Collision leave. Hope it helps.

How to detect properly when the player can crush an enemy from above?

This question is much more complex than it seems, here's why.
I have a player and the physic is enabled, in a 2D + 3D world.
The monsters have a sinusoidal movement = they go up and down, from right to left.
The player can only jump more or less high, but can't move right or left (like the dino game on Chrome when there's no Internet connexion).
After many unsuccessful attempts to detect if the player is above (it's not simply if (PlayerY - MonsterY) > 0 otherwise I wouldn't ask here), the best idea I came up with is to make two colliders in the monster: one for the "head", and one for the "body".
When the player hits only the head it can only be above (otherwise it's the body first), so it's an easy situation
When the player hits only the body it can only be below or on the sides, so it's an easy situation too
sometimes, it happens that the player hits both colliders.
So I'd like to check this after the FixedUpdate calculations.
If you read the execution order of the callback functions, there's no LateFixedUpdate: even though there's a LateUpdate, it's officially written that the developer should handle physics in FixedUpdate, and nowhere else.
What I've done so far is: when the OnCollision of monster head is called, just change a boolean, and same for OnCollision of the monster body. So after the OnCollisionXXX, like the documentation says, there's only one thing you can use: yield WaitForFixedUpdate.
I'd like to check those booleans and act according to their values in the yield WaitForFixedUpdate.
I can't find any valuable example on the net on how to do this. Any idea how to implement yield WaitForFixedUpdate in this situation? I'm stuck.
To the question
how to implement yield WaitForFixedUpdate
The yield is ment to be used inside of a Coroutine
yield kind of reads like "interupt the routine here, render the current frame and continue from here in the next frame."
e.g.
private void Start()
{
// Start the routine once
StartCoroutine(LateFixedUpdate());
}
private IEnumerator LateFixedUpdate()
{
// looks scary but is okey in a Coroutine
// as long as you yield somewhere inside of the loop
while(true)
{
// Continue after all FixedUpdate has been called on all scripts
yield return new WaitForFixedUpdate();
// do something
}
}

UE4 Objects overlap position after collision

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.

Spawning a projectile in ImpactJS

Using the example code that came with ImpactJS, I am wondering what
{direction:this.lastPressed}
means. The code below refers to the player entity, when the 'attack' button is pressed.
Is 'direction' a method of some sort? I am guessing from this code, it is telling the projectile which way to travel based on the entity's direction, but I can't figure out how.
//attack
if(ig.input.pressed('attack')) {
if (this.weapon == 'projectile'){
// create a projectile
ig.game.spawnEntity('EntityProjectile',this.pos.x,this.pos.y,{direction:this.lastPressed});
}else{
// we simulate a sword with a very fast moving projectile with a limited range
ig.game.spawnEntity('EntitySword',this.pos.x,this.pos.y,null);
}
ig.game.sortEntitiesDeferred();
}
I decided to go back to basics with this and use the code for this game to base mine on. This is a lot more straight-forward, especially for the first time game developer.