RigidBodies without Extra Physics Behavior in Unity - unity3d

Currently I'm making a racing game.
I move my cars with the Transform.translate class.
Now the thing I want for my cars is not to move through each other.
I attach colliders and a RigidBody to my player car and it's working.
But my problem is that each time my CPU cars and player car encounter with each other, my player car shows unrealistic behavior like moving out of the screen or throwing away.
I know this is part of the physics engine behavior but is there a way to make the RigidBody only do one job and make objects not to move through each other not to add other physics behavior?
Any ideas?

There are just few problems:
1.You don't move Rigidbody with transform.translate. When you do this, colliders will go through other colliders.
You move Rigidbody with Rigidbody.AddForce, Rigidbody.velocity and Rigidbody.MovePosition. By moving Rigidbody with these methods, collision should be functioning properly.
2.You are not even supposed to move the Rigidbody of the car.
Use WheelCollider. There are many online tutorials on how to set them up on the internet but Unity's documentation is fine too.
WheelCollider.motorTorque is used to move the car forward or backwards.
WheelCollider.steerAngle is used to steer the car.
WheelCollider.brakeTorque is used to brake the car.
If you need further help, you can find a fully working WheelCollider samples here.

Related

How to deal with nested rigidbody?

I can't seem to wrap my head around the following, I have a simple spaceship with 2 turrets:
My 'Player' the white ship has a rigidbody. The turrets have not.
I'm moving and rotating my player via the rigidBody, however I would like to also rotate the turrets via the physics system.
I thought about adding a rigidBody2D to the turrets, however that was not recommended from my understanding reading posts online. This is because rigidbodies in children would affect the parent in unforseen ways.
So dropping that idea, How can I move my turret and having it be affected by physics?
Some sidenotes:
I want the turrets to be influenced by physics. Lets say i try to rotate my turret, but it hits a wall, I want it to resist. Lets say some debris hit my turret's barrel, I want it to move in the opposite direction.
Although i could move the turret outside of its parent's container (the ship) I do ofcourse want it to 'stay on the ship in game'. The turret is part of the ship, its mounted to the hull or fuselage if you will. So it should move with the ship and keep its position.
As mentioned by derHugo joints where up for the task, although i had to restructure a bit, here is the outcome:
I added an empty transform to hold the ship and its sub-components, so turrets are no longer part of the ship parent, so that i can add a rigidBody to these child objects now.
Then I added a hingejoint and rigidbody to the player ét voila, it now works:
I still need to take a good look at how joints work exactly but this seems to work.

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine

Unity 2D how to avoid my player moving over the enemies

I'm doing a platformer and I have colliders and rigid bodies in my hero and my enemies. I have also colliders on my platforms.
Everything works and moves nice, dudes move along the platforms, they jump and they catch each other.
I use the collider in my enemies to discover if the hero touches them and then deal damage to the hero. And when slashing I use "overlapCicle" to discover if the sword touched the enemies.
My problem is that with this setup my hero and my enemies can walk one in the top of others. Additionally if I disable the collider of an enemy (to make it invulnerable for a second after being hit) it will fall through the platform..
What's the best approach to this structure of colliders? I want everybody to walk over platforms. I want enemies colliders to detect the hero touching them and I want my sword (overlapcircle) to find enemies. And I want hero and enemies to be able to walk across each other, specially enemies.. they should not walk one over the others
Seems like you want the enemy and the player to be able to walk through each other, while being able to interact with each other.
In that case, you can create another physics layer for your interactions (attacking or vision detection), and set-up your Physics layers to ensure that the Player and Enemy do not collide, but their interaction layers can collide with the respective characters.
Like so:
Where PlayerTriggers and EnemyTriggers will be physics layer for interaction between the enemy/player.
This ensures that the enemy's vision/attack collider can hit the player, but the enemy itself can't do so. Vice versa.
To access the physics layer menu, go to Edit (Top left) and click on Project Settings.
You can read more about it in Unity's Doc.

Should we never have a collider component on a game object unless we also have rigidbody?

After watching this (from 13:12 to the end) official Unity tutorial I have a question: Does this mean that ideally we should never have a collider component on a game object unless we also have rigidbody? Because if we only have a collider, Unity will consider the game object as static. So we should always add rigidbody and indicate that it is Kinematic.
It is perfectly fine to use colliders on static objects that don't move without adding the rigidbody. It is even better for performance as these colliders are cached in the physics engine.
There is no point in adding a Rigidbody to lets say a floor, that will never move.

Do I need a rigidbody for gameobject that scales up and down?

I have a beginner-like question. I currently have a gameobject in my scene that scales up and down via animation. It has a Circle Collider 2D on it.
I've seen some tutorials before regarding the performance optimizations of rigidbodies and colliders. I learned that if the gameobject should move in the game, it should have a rigidbody component. Otherwise, a collider component itself is fine for triggers.
Since my game object is kind of moving (because of the endless scale up/down animation), would it be best to put a rigidbody component on it?
I do like to mention that I'm not using any physics movement such as AddForce or anything like that. Hope someone can clear this up.
A gameobject should have a rigidbody component for mainly two reasons, first one is if you are using physics, second one is if you want other colliders to detect a hit/entrance/exit by that gameobject.
From what you described it does not seem like you would need a rigibody on your gameobject, especialy because the movement is only by scale and not by position.
and also, just to clarify, putting a rigidbody component on a gameobject is not a must even if the gameobject is moving. there are different ways to move objects, and rigidbody usually helps in case that the movement should be very realistic and interact with other gameobjects it is colliding with.