Unity3D: Recalculating collisions after Physics2D.IgnoreLayerCollision()? - unity3d

I have my "player" gameObject standing atop a platform. He press a combination of keys, I verify that he's standing on the right kind of platform, and I run this code:
Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
The intention being that he'd drop down from the platform. Instead, nothing happen immediately. The player is still atop the platform and can move around - only if he gets out of it and tries to get back to it, only at that point will he fall down from it.
So I made a temporary ugly workaround:
Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
rigidbody2D.AddForce(transform.up * jump / 1.5f);
This will force the player to jump, making Unity "recalculate" the collisions and the player will, as I want to, pass through the platform. The even weirder part is: if the jump isn't high enough (100 force at 1 mass with 0.5 gravity scale seems to be the minimum), the player will still land on the platform, even though Unity is supposed to be ignoring those collisions.
I also tried pushing the player downwards, both with force and direct velocity, but no luck - he still collides with the platform, he only stops colliding with it after being away from it once.
Also, my player Rigidbody2D Detection Mode is set to Continuous, and I tried setting the platform up in many different ways, with and without a Rigidbody2D.
Any ideas on how to make the player instantly fall down from the platform as soon as collisions start being ignored? Thanks in advance.

I had been having the exact same issue. I have an alternative solution that you may find to your liking.
Rather than disabling or moving the terrain to be ignored, temporarily set the rigidbody2D of your player gameObject to kinematic. This will not cause you to 'drop' through the platform as you would like - but it will allow you to create the illusion that you are, by briefly using transform.Translate (or Vector3.Lerp, if you like) to move your character down via code.
This will disable Rigidbody2D.AddForce, but will still allow for set movement via rigibdody2D.velocity, if that is preferable to manipulating the transform directly.
Ensure the downward velocity lasts only as long as you are Kinematic, and try to let it match the gravity of your game, and it looks pretty seamless.
If you turn the rigidbody back on too quickly, it can (even with ignoreLayers :/ ) still jump back up to the platform - but if you leave it on too long, you can drop through more than one intended one-way platform, or even :O through the bottom of the stage!
This is easily avoided if you can be careful - personally I used both MonoBehaviour.Invoke to set a very short time limit on how long the player would remain Kinematic (about 0.5 seconds), AND I disabled Kinematic as soon as they moved 0.5 units from the point where the 'hopDown' was initiated.
If your gravity / platforms / character scale are vastly different than my own, you may have to experiment with some different numbers.
This has been my first attempt at an answer on stackoverflow, so I hope it has been helpful!

One easy way to do this is after calling
Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
You reset the player's layer:
player.gameObject.layer = playerLayer;

It's sounds like you have covered a ton of possible solutions to this problem before coming here so, good job there. I listed a few alternative ideas below. I hope they help. Good luck!
Remove the collision object from the platform itself.
Offset the collision object to be in some remote location so it will not interact with the player. Offsetting the object may cause the collision to have a similar reaction to the jump you mentioned.

Related

Why does using PhotonRigidbody2DView give 'elastic' movement?

I'm currently using photon pun 2 to learn simple 2d multiplayer.
Using PhotonRigidbody2DView is giving an 'elastic' movement to the player. The player goes forward a bit and then returns to the original position.
Here's the video:
https://youtu.be/HgFVsofVZcQ
Why does this happen and how do I solve this?
I tried using PhotonTransformView and PhotonTransformViewClassic but it is giving weird results. The players go inside each other and when one player collides the other, the other player starts jittering.
So I decided to use PhotonRigidbody2DView instead. Now the players don't go inside each other and the jittering is also not happening but the 'elastic' movement problem is happening.
This happens because the remote client uses the velocity of the rigidbody to reproduce the movement of the character is does not control. This makes it a bit independent from the lag but for arcade style movement (where direction changes are immediate), this doesn't work all that well.
Solutions to this depend on what you actually need. Networked objects combined with physics can be tricky to get right. For PUN 2, we didn't implement a solution to this case and assumed you'd tweak the PhotonRigidbody2DView as needed.
The Smooth Sync package in the Asset Store seems to do well and is a plugin to PUN 2.
Alternatively, the newer Photon SDK "Fusion" should do better and is state of the art as networking solution.

Recommended strategy for overriding certain Rigidbody collisions in Unity?

I'm looking for a little newbie advice on gamedev strategy and/or approach. I'm working on a game that uses Rigidbody physics in Unity and I've got a character interacting nicely with physical objects in the game world. The thing I'm trying to work through is that there are certain physical interactions that I want to control, but my own attempts to control them are conflicting with what Unity is already (correctly) doing.
For simplicity, let's just say I've got a Rigidbody-based player, and when certain Rigidbody-based objects collide with that player, I want to override where they deflect to. When I detect the collision and apply my own force with rb.AddForce(myForceVector, ForceMode.VelocityChange), Unity is also applying it's own force as a result of the same collision. I also tried setting the velocity directly with rb.velocity = myForceVector, which mostly works, but there are still situations where Unity applies force after I set the velocity so it's glitchy at best.
Other options I've considered are:
Use an additional, larger IsTrigger collision mesh to detect and handler the special type of collision I'm looking for before the actual collision occurs. That might work for slower speed projectiles, but likely will have the same issue for faster collisions.
Change the player to not being Rigidbody-based, which might work, but would require me to code a lot of the other interactions that are already working out of the box with Rigidbody physics.
Use RayCasting to detect when these special collisions might occur next and handle them before the actual collision. This is where I'm leaning currently.
Anyone have a recommended best practice for this kind of thing?
Thanks in advance!
The easiest way might just be to take full control. On the correctly timed press, disable the RigidBody and the Collider. Then just move it yourself to the target location. It's really hard to get precise control over a RigidBody -- every FixedUpdate(), it might be picking up force from collisions, and there can be many FixedUpdate() calls between Update() calls.
You could write a pretty simple coroutine using a Lerp() or Slerp() between the object's current and target position. That way, you could fully control how the object moves toward its target.
Just to follow up... I attempted to "take full control" as suggested in Alex M's answer above, but that ended up making a number of other things complicated in my specific situation. However, his comments about disabling the collider in the original question led me down a different path that ended up working well...
I ended up disabling the collider for the deflecting rigidbody upon impact and setting the velocity of the deflected body directly. That way, I was able to avoiding any additional force(s) applied from Unity while the collision was active. Once the deflected object was on it's way, I just had to reenable the collider.
Thanks, Alex!

Character Pawn sliding back when picking up objects

I am using Unreal Engine to create a puzzle game. In this game, the player has to pick up a chair/table and place it on a pressure plate in another part of the room in order to unlock a door.
When play testing, picking up objects causes my character to slide back. Once the sliding starts, there is no way to make the character stop sliding. I suspect that this issue may be caused by the object's collision volume entering the character's. However, when I release the object, my character keeps gliding backwards even though by then, the object's collision volume is out of reach.
On my character's blueprint I have locked rotation on all axes, so that it won't randomly start rolling backwards after picking up an object. This solves part of the issue but there is still the gliding that just won't stop.
I am new to UE and I have no idea what may be causing this issue. If you have any previous experience in this or simply want to chime in, please do!
Another alternative fix is to lock the rotation of the collision component in the default pawn that you have or to increase angular damping.
To do so, open the default pawn blueprint and under the physics section, then constraints, lock rotation in the X, Y, and Z axis. Or increase angular damping.
Dont know why this is happening exactly unless we see the actual settings you are using and the BP but when you pick up the object , disable its physics and collision and when you put it back enable it after a delay of 0.5 or 1 second . Hope this helps.

How to restrict which colliders are triggered?

Sorry if similar questions have already been asked.
I have a character that can hold up a shield to block incoming damage. The character has a circle collider for his body space. The shield has a box collider that blocks off a portion of whatever direction he's facing, and it's only enabled when the player is holding down a button. My enemies have weapons surrounded by triggered box colliders which are enabled when they decide to attack.
So, my problem is that when my character is attacked while shielding, sometimes his body collider is detected and sometimes his shield collider is detected. I can't find any consistency no matter what I try.
[Screenshots] (http://i.imgur.com/VHujbcG.png)
[Code] https://gist.github.com/siketh/2401454977d10ed7699b
I've been struggling with this all day and need another set of eyes. This isn't a serious project so if you need to see anything else I'm happy to post more code or explain more of my design.
It's probably because the weapon intersects with both the character and the shield at the same time. There are great tools in Unity if you want to see if that's the case.
Notice that Unity has "Play", "Pause" and "Play one step" buttons at top of the editor window. You can pause the game with Debug.Break(); instantly at the moment player fires or swings the weapon. Then you can watch what's happening step by step. Select the character, the shield and the weapon so that you can see their colliders in action. This way you can see what collides with what exactly step by step.
Similarly you can see the action in slow motion by adjusting time scale. Something like Time.timeScale = 0.1f;
To prevent this,
You may want to look at continuous collision though I've always failed to use it properly in Unity.
You may want to utilize more physics layers. http://docs.unity3d.com/Manual/LayerBasedCollision.html
You may want to detect collisions by using raycasts.
You can enlarge the shield and reduce the weapon swing speed or bullet speed. So that it's impossible to penetrate through shield.
You can decrease the period of FixedUpdate calculations so weapon movement will be smoother and there will be less penetrations.
http://docs.unity3d.com/Manual/class-TimeManager.html
There is a high probability that you are detecting both weapon-to-shield and weapon-to-player collision in one step. So why not postpone the decision one FixedUpdate later. So that you can check if there was only weapon-to-player collision or both collisions. If weapon-to-shield collision detected in previous update, then you know player hit the shield no matter if weapon passed through the shield or not.
Let's say there is no way you can detect if weapon hits the shield or the player. By using some simple geometry, you can check if the attack is coming from the direction of the shield or not.
Good luck :)

Sudden frame rate drops on mobile when instantiating/destroying?

I am currently working on a mobile game and I can't help but notice that whenever an object (an obstacle in my case) is instantiated or destroyed, I get a sudden FPS drop which is critical for my gameplay.
To help give an idea, I instantiate obstacles on top of my screen every 1.5 seconds, then I scroll them down. If the obstacles reaches the bottom of the screen already, I destroy them to prevent memory leaks/waste.
I'm still pretty new to Unity development. Am I on the right track though? What is a better solution to prevent this sudden frame rate drop?
Do you have any big / nested loops or complex processes going off as part of instantiation (Look at your awake/start methods)?
Regardless, look into object pooling as a better method to handle this type of thing.
For a basic example, instead of creating/destroying projectiles of a gun every time it's used, give that gun a "projectile pool" that creates n projectiles when the level loads. Then, when shooting, just set the projectile's position back to the gun and set the projectile as active. After impact, have the projectile deactivate (or after a few seconds if nothing is hit).