Custom Spawn Functions - spawn rotation - unity3d

I'm trying to do some object pooling in my networked game.
I'm following this piece of documentation.
The issue is: how do I set the right rotation to the pooled object?
The delegate gives me the spawn position, but no rotation.
The objects I'm pooling don't have synced transform.
Any solutions/ideas?
EDIT:
I gave Unity feedback regarding the SpawnDelegate signature that is the root of my issue.
https://feedback.unity3d.com/suggestions/spawndelegate-signature
EDIT2:
I read a bit the decompiled code of UNET and maybe the solution could be customizing the serialization/deserialization of the objects and adding the rotation (OnSerialize/OnDeserialize). Adding a SyncVar would be the same I think but at an higher abstraction level.
From the software engineering standpoint I don't like the idea of adding a component for this basic functionality.
EDIT3:
This is the UNET decompiled repo. I cannot understand how the rotation is set correctly when spawning with default spawners. For default I mean when you register the prefabs with ClientScene.RegisterPrefab

Related

Directional Friction

I want to have the friction of a Physic Material depend on the direction. I found this documentation from Unity 5.2 that lets you set two directions and friction coefficients but that is missing from the newest api. Is there a way to do this in the new version or am I going to have to do it manually?
I'm trying to use wheel colliders to provide similar functionality but they are behaving very strangely. They fall through the floor, jump unexpectedly, bounce weirdly.
This is a very good question as I was having the same problem a while back!
Unfortunately, what I have gathered from both unity developers and the current documentation is that the feature you described is not implemented into the current Unity engine as you have assumed.
Some work arounds that I would suggest is reverting back to a previous version of the engine if you are able or create your own friction system that would take data from the velocity vector of a physics object to determine which direction it is travelling. More information on finding a Physics object's velocity can be found on this page https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
Best of luck on your Unity Project!

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.

Why do my 2D objects not collide in Unity despite that the layers are set correctly?

I have a platform that I need to flip the direction of when it collides with an object. The platform as well as its child objects belong to the layer "FlyingOrPlatform". The object that the platform collides with should belong to the "GameControl" layer. But if i set it to that it doesnt collide with it.
If I set the object that the platform collides with to either the "Player" or "Default" layer. Then it works completely as it should. But I wish to use the "GameControl" layer.
I have tried where I save the scene and the project, as well as restarting the editor.
I dont see what should be wrong with my layer setup. Any suggestions?
In my script for reversing the platform direction there is no layer check, it does check the tag but I ensured that the tag is the same when I switch between the layers. I just use Unity's built in OnTriggerEnter2D lifecycle method. Have tested with OnCollisionEnter2D and OnTriggerStay2D as well, same result.
Here is a link to a video demonstrating the issue, as well as a screenshot of my physics2D settings below. In the video I first show how its supposed to work, and then I switch the object the platform collides with to the GameControl layer, after which it doesnt work.
Video of issue: https://www.youtube.com/watch?v=l-eqYI-K-Ug
Physics2D settings:
From what I can tell this appears to be an issue with the Physics2D matrix in Unity that occurs from time to time. A couple of other people have written similar things on other forums.
Among others this reply had a similar issue:
For lack of better, I found a work-around by using the "Ignore Raycast" layer instead of the "GameControl" layer.
I have reported it as a bug to Unity.
EDIT:
Here is the reply I got from Unity.
"Hi,
We successfully reproduced this issue. It will be possible to follow
the progress on a chosen resolution in our public Issue Tracker once
the report has been processed (this might take up to an hour):
https://issuetracker.unity3d.com/product/unity/issues/guid/1325018
We highly appreciate your contribution. If you have further questions,
please feel free to contact us."

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!

Improvement for a lightweight and rapid object interaction using Unity

I have a question about the interaction of objects with each other.
Especially with regard to the application on Hololens 2 and the computing power that comes with it.
What would I like to do:
I have two objects. One is a simple staff and is moved by the player / user of HoloLens 2. The other object is a complex construct into which the staff shall be inserted.
Current behaviour:
After adding a BoxCollider and Rigidbody component to the staff and a MeshCollider to the complex object, I was able to create some interaction. Unfortunately the whole process is so expensive that it causes the HoloLens to crash.
My question:
Since I don't need any physics, but only pure non-penetrable objects, does anybody have a hint how I can achieve better performance?
Or what I have to change completely to make it as fast and lightweight as possible?
Notes:
Most entries online that I have found related to firing events or triggers that I do not need at all.
Even in PlayMode on the local computer it stutters.
Due to the complexity of the mesh I cannot simplify the target object.
Any help is welcome.
You could always decimate the complex object in something like blender: https://docs.blender.org/manual/en/latest/modeling/modifiers/generate/decimate.html
Import the decimated object into unity then use that mesh for the mesh collider instead of the rendered mesh.
That should allow you to simplify the mesh so it's more performant but still be plenty complex enough to closely follow the geometry of the original model.
I've used a similar method to reduce improve shadow rendering performance quite effectively.