How to use CharacterController and BoxCollider simultaneously? - unity3d

Here is the problem, I have a troop controlled by CharacterController and I want the troop to block if collide with something. So I add a box collider to it. But it's not working, as the figure shows. The cube doesn't block the BoxCollider but does block the CapsuleCollider in CharacterController.

Short answer: It just doesn't work.
Character Controller is always using its own CapsuleCollider and there is nothing you can do about it. It's a feature requested for several years, but there doesn't seem to be any interest to add this feature in the near future.
You need to replace the CharacterController with a Rigidbody and write your own controller (or copy&paste one) to handle movement. Basically you use Rigidbody.AddForce to move your player.
If you need some ideas how to implement it, have a look at these tutorials. They all use a different approach to control movement:
http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player
http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player
http://unity3d.com/learn/tutorials/projects/stealth/player-movement

Related

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.

Unity, OnMouseOver blocked by another gameobject with a collider in front of it

I have two gameobjects, both with 2D colliders.
One of them can be behind the other, and because of this its OnMouseOver can be blocked from firing as the GameObject in front blocks it from triggering.
What is a way around this? I really like the ease of using OnMouseOver, and would rather not use raycastAll.
You have 3 possibilities:
Use RayCast.All() but you said, that you want to avoid that.
Set the "blocking" GameObject to the Layer "IgnoreRayCast"
That answer has a third possibility.

Unity not working collider in race game lap logic

I am making a racing game and I have created points 'round the map that you must pass in order to finish a lap.
Sadly, when the player passes the first part it doesn't indicate it and so it can't pass through the other one.
I am using OnCollisionEnter() but IsTrigger is activated on those points so that they have no collision.
I even added Debug.Log(""); to check if it actually does something but it wont show anything in the console. Here is the collision code in the car:
function OnCollisionEnter(col: Collision) {
... never gets to here
}
Note: Don't tell me to switch to C# because JavaScript(UnityScript) is being remove, I am well aware of that.
I believe your basic problem is you should be using
void OnTriggerEnter
instead of the OnCollision calls.
It is a situation where this annoying thing...
http://docs.unity3d.com/Manual/CollidersOverview.html
will solve your problem. Scroll down to:
"Collision action matrix"
You have to familiarize yourself with that, to use Unity. It's a pain.
To get an answer on SO, for specific cases of this type of problem, you need to provide the answers these eight questions:
on the "CAR"
rigidbody? / kinematic on or off? / collider ? / trigger on or off?
on the "WALL"
rigidbody? / kinematic on or off? / collider ? / trigger on or off?

Unity3d move object with some force

This is what i try to achieve in 2D:
A GameObject
A button
A scene
1) I push the button
2) The GameObject comes in from the side like someone are throwing the GameObject into the scene meaning it have some speed that blends off and stop
I know i need to use RigidBody2d but i do not get it to work. Could someone please give me a hint how to solve this?
You may use free libraries like iTween that allow you to move objects in 3d or 2d space. With iTween making it appear "outside" the screen and move while slowing down to a certain point is easy. Or do you want to use physics for that?

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