Using triggers attached to enemy to make enemy jump in a 2.5d platformer - unity3d

so I've got an enemy in my 2.5d platformer, that I already got to follow the player around, now I want him to jump over small obstacles, and honestly, I have no idea how to. My enemy is a sphere with rigidbody and a box collider scaled 2x in X axis, effectively making it 2 times wider than the sphere. I want this box collider to be a trigger and for it to launch a AddForce.
Where do I put the code for the trigger - if I put it in void update() wouldn't it apply force until the sphere is above the obstacle? I only want it to apply the force once.
Also, what would the code for the trigger look like?
So far I only got figured out how to find the rigidbody by getcomponent and apply force to it, and I watched a video on triggers, but it didn't really help :/

Use OnTriggerEnter method like this:
void OnTriggerEnter(Collider other)
{
if (other.tag == "IMakeEnemyJumpBeforeAnObstacle")
{
// add force here
}
}
Note: this code should be in your enemy script. Plus use IMakeEnemyJumpBeforeAnObstacle tag on the trigger object which will be placed right before the obstacle. And make sure both the enemy and the other object have property IsTrigger checked from inspector.
Learn More
Hope this helps

Related

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

Raytracing not responding correctly

So I'm new to Unity and I'm sure I'm missing an easy step, but after looking online for a while for some reason I can't find the solution.
I have two objects on the screen, player and enemy. Both have Rigidbody2D, and Box Collider 2D attached. On Box Collider 2D I have clicked is triggered On Rigidbody2D for both I have clicked Is Kinematic. On player I have a simple movement script. On the enemy object I have this:
void Update () {
RaycastHit2D hit = Physics2D.Raycast(transform.localPosition,transform.right,Mathf.Infinity);
Debug.DrawRay(transform.localPosition,transform.right);
if (hit)
Debug.Log(hit.collider);
}
Now for some reason when I move player over the object if (hit)
is true, but if I move the player anywhere on the right side it is not true. What would be the reason for this? Thanks.
First of all you don't need Rigidbody for raycast detection, only colliders. Second, Physics2D.Raycast uses world position, not local, so replace "transform.localPosition" with "transform.position", this messes it up a lot if the transform is child of something. Take in mind that you are sending raycast from the right side of your transform, so maybe it's not hitting anything and values you are getting are actually correct.

Unity 2d 5 Collider not working

Using Unity 5.0.1f1
I'm trying to make it so that when I shoot, if it hits an invisible object it destroys it, but when it collides nothing happens. Here is the code:
void OnTriggerEnter2D(Collider2D col){
Destroy (col.gameObject);
Debug.Log ("find");
}
It's hard for me to give you an answer given the little information that you are providing. But, the most possible solutions to your problem are:
Attaching a Rigidbody component to one of the colliding objects.
Making the object which will be destroyed have a normal collider attached and the other object must have a trigger collider attached.
Make sure that all of the Colliders/Triggers and Rigidbodies you use are 2D. That is, Rigidbody2D and Collider2D. Because you are using the void OnTriggerEnter2D method.
Make sure that the colliding objects are in layers which collide with each other. You can check which layer collides with each layer by going to Edit-->Project Settings-->Physics2D (Or Physics if you end up using 3D Physics).
Hope this helps!

In Unity3D How to Detect the Collider Touch and Stay In Other Collider?

I need to check if the sphere collider still stay in the mesh collider.
now I used:
void OnCollisionEnter(Collision info)
{
Debug.Log("enter");
}
void OnCollisionStay(Collision info)
{
Debug.Log("stay");
}
void OnCollisionExit(Collision info)
{
Debug.Log("out");
}
here is the scene:
the onCollisionStay() will execute per frame ,but when the scene came to before,the Debug do not log continue,but the "Out".
So I need to Know Why the Stay event do not Stay and How to detect One Collider
Stay in the another just inside and touch?
Increase the size of Sphere collider or use 'Delay' option in Collision Stay function. Your question is bit clumsy, What your are trying to do, tell it clearly.
Can you give more details of the mesh collider? It might happen that the collider is pretty small also, and in some point, a part of the sphere (not the full sphere, so you think OnCollisionStay should still be called) goes out of the mesh collider, thus activating the OnCollisionExit(), but since the OnCollisionEnter() is not called back, OnCollisionStay() is never called again.
Maybe you can check that.

Raycast passing through box collider when trigger is enabled for object

I'm setting up footstep sounds in a 3rd person game. All works fine, except getting water collision to work correctly. I setup a simple box collider where the top of the box is on the surface of the water and the bottom of the box is below the terrain and water. However, when I set the collider to a trigger the player passes through the collider (correct) but the raycast also skips the collider and hits the next object which is the terrain (raycast is checked on in settings to collide with triggers..). of course if i turn off the trigger the player walks on the box collider, which is not what I want. The transfer.position is the in chest of the player model. the water comes up to the model's waist. Any help is appreciated. Thanks.
void Update () {
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit))
{
Debug.Log(hit.collider.tag);
}
}
Ideas to try:
1) Check Edit->Project Settings->Physics, make sure Raycasts Hit Triggers is checked (looks like you already did, thumbs up).
2) Make sure that your trigger is not on the Ignore Raycast layer.
3) Make your trigger thicker/wider.
4) Give your Raycast call an explicit layer mask and make sure your trigger is on that layer.
5) Make sure your trigger component is actually enabled when the call goes through.
6) Try the other *cast methods... Collider.Raycast, Physics.Spherecast, etc.
7) Solve it a different way, like using another Collider specifically for this purpose on your character object, and using OnTriggerEnter or collision events.
8) I vaguely recall having a similar problem a long time ago, and the solution involved putting a dummy RigidBody on the trigger object... something about Triggers getting filtered out in certain cases when no RigidBody is attached.
Hope that helps!