Child Components with rigidbody - unity3d

Context
I have in my project two elements. A player (just a cube) and some eyewears.
When the glasses aren't attached to the player, i want them to have properties of a Rigidbody. But when the eyewears are attached to the player, i want them to be a static object, so the player can process the collisions and physics.
I have tried:
DestroyElement(rigidbody) when the player pickup the eyewears. When he leaves them, i recreate the rigidbody with AddComponent
It worked nice, but in the future other elements will be attached and they will not share the same properties of rigidbody. I though maybe i could save the rigidbody instance, so when the player leaves the glasses i assign it to them. I could't.
AddComponent don't accept arguments.
Then i tried to set "kinematic mode" when my player wears the eyewears. It didn't go well, my player can't jump anymore and sometimes he glitches in the floor.
How can i resolve this?

GameObject.AddComponent does take an argument, or a generic argument (preferred):
go.AddComponent<RigidBody>();
this is also possible, but deprecated, since you lose type-safety:
go.AddComponent(typeof(RigidBody));
However, RigidBody is not meant to be added/removed, and in your case I would say that kinematic mode is the way to go... but I can't tell why you're experiencing weird results with it.

Thanks to R Astra i checked again the eyewear collisions and i found out the problem. I had enabled the Convex mesh.
The col was causing trouble because the back meshes were inside the player
So, quickly i copied that eyewear and generate a new mesh. It worked!
Thank you very much!

Related

When i add rigidbody, animation makes player float. In Unity

I am trying to play the animation but whenever i add rigidbody, player starts to float in the air at the constant value (which is 3.5 Y). No script is attachted atm.
I tried to remove rigidbody which solved the problem, but i need it so deleting rb is not an option. Gravity values are normal and the other animations are from mixamo (i didn't do the animations).
Based on the information provided here is what I'm thinking:
The root cause of this is that your character model when effected by gravity is being pushed to the ground and foot IK is causing the legs to end up looking like your screenshot above.
Some common approaches to this problem:
Create an empty object called player (Controls Rigidbody/Capsule Collider/Character Logic)
Add the 3d Model as a child of this object (Animator/Mesh/etc).
On the root object add your rigidbody as well as a capsule collider (prevents character model from colliding with the environment due to gravity).
With this setup animations can be done on the model (+foot IK) without gravity causing issues.
Okay i just solved the problem. Problem was neither rigidbody nor animation.
The collider of mesh (ground) has option checked "is Convex". I turned it off, problem solved, player is not floating anymore.

Prevent pushing while still detecting collisions in Unity

I am making a 2D game. There are 2 characters, that can shoot bullets into each other and walls. I need to detect collisions between bullets and characters, so bullets and characters Rigidbody2D type should be dynamic. I need to prevent characters from pushing each other, but i have no idea, how to do this, without changinh their Rigidbody2D types. Making all them triggers doesn't work, beacause it will make walls passable. Help me please.
The best solution and something you should start getting use to is putting different collider groups onto different layers and then setting which ones can collide with each other in the project settings panel.
Edit -> Project Settings -> Physics2D
The sort of layer setup I think you are looking for
Try making the Bullets triggers, then in a component on the Bullet object you can Destroy(gamobject) or otherwise redirect the bullet in its OnTriggerEnter2D method.
That would mean walls are impassable, and bullets don't push anything.

using unity physics with SteamVR

I want to make a plank game in VR with Unity. So when the player walk outside of the plank, he falls. Right now the only way to make it work is by using VRTK which is another physics system and it makes a lot of things complicated.
I've put a rigidbody on the CameraRig and uncheck "is kinematic". The player falls, but the colliders on other objects are not working anymore...
Is there a way to use Unity's physics with SteamVR and without VRTK ??
Thank you !
Firstly I would read up on Rigidbodies and Colliders/Trigger Colliders - here's a link.
Here's a useful table from that website:
You will need to use this to understand why the player is falling. Is the CameraRig actually colliding with the ground? Is it a Trigger Collider (which has a callback method but doesn't do any physical collision). There's many possibilities for why.
I wrote a script that you can drag in two objects and see if they collide. You could use that if it helps.
The issue in VR with Vive is that determining where someone walks can be difficult, as we are only tracking their head and their hands. If you have a Vive Tracker available and it fits your use case you could use that to track someones foot.
What I have done in the past is use the Camera(eyes) GameObject within the CameraRig and get it's transform.position.x and transform.position.z value to determine if it has gone outside of the boundaries of the object the user is standing on.
Hope this helps,
Liam

Determine on which collider the collision has taken place

I have a gameobject with two sphere colliders attached. One has IsTrigger checked and the other not.
I want to execute different set of statements when collision occurs with different colliders. For example I want to play different sound for both different collisions. Is there any way to achieve it?
I tried OnTriggerEnter() but unfortunately it is called for both type of collisions since other colliding objects have triggered colliders. I just thought if we could somehow find out on which collider of the gameobject the collision has taken place we will be able to achieve it.
So is there any way to get through with this?
I have been using Unity for years and faced tons of problems like this, related to bad software design. I hope Unity guys will handle physics more carefully in future releases.
In the mean time, you can use Physics.OverlapSphere and Physics.CheckSphere to manually check if there is something that collides with your object. Remove the collider that you are using as a trigger and use these methods instead of OnTriggerEnter. This is a bit hacky, but this will do the job I think.
Make your colliders visible in the inspector (make them public or add [SerializeField] before it) and then tie in the colliders to the code that way.
Then, in your collisions, compare the colliding objects against your variables that are holding the colliders for you to keep them separate.
To detect for source trigger in OnTriggerEnter, you must use workaround with multiple gameobject hosting trigger and satellite scripts.
Allow me to link to my answer on gamedev SO:
https://gamedev.stackexchange.com/a/185095/54452

Getting Skybox to change on collision in unity with UnityScript?

I'm quite new to UnityScript as my area of interest is python, however me and a friend are planning to create a small indie game and I need the skybox to change on collision in unity. I would prefer this to be done in javascript if at all possible. Please take a look at it and let me know whats wrong with it, as when run it makes no difference to the scene.
#pragma strict
var mat:Material;
function OnTriggerEnter(trigger: Collider){
RenderSettings.skybox=mat;
}
That is the entire script. Thank you for any help given
make sure that for your objects that collide... typically one must have a static collider (e.g. floor, wall) and the other a collider and rigidBody to hit it (e.g. player, car). Also check your physics settings to make sure the layers that the objects are in, can hit each other.
Make sure you have your script on the right object... collider or collidee ? try putting a script on both objects with a print in each.
Check here for more info
http://docs.unity3d.com/Documentation/Manual/Physics.html
What exactly to you mean by changing on collision? Skyboxes cannot be collided with. If you mean objects colliding other than the skybox, make sure that at least one of the collider's has "Is Trigger" checked in the inspector.