How to spawn a collider after exiting a trigger? (Bomberman style game) - c#

I trouble trying to get this to work. here is the problem.
When I spawn I bomb with a collider, it presses my character out of the way, forcing him either in the wall or to a side.
Now my character instantiates a Bomb gameobject with a bomb script.
What I want and my thought process:
I was thinking it might be possible having a triggercollider on the instantiated bomb, and no collider, but when my character "leaves" the bomb radius it spawn a regular collider.
Then He can't get stuck, and when he has exited he can't just past through it again.
But I don't know how to write this, any suggestions?
Or any better ideas? Thanks for all help :)
ps: ( I use c#)

You almost get a solution.
1. in bomb collider check trigger;
2. in bomb script at start get reference to collider
3. in OnTriggerExit set isTrigger to false
public class bomb : MonoBehaviour {
BoxCollider collider;
void Start () {
collider = gameObject.GetComponent<BoxCollider> ();
}
void OnTriggerExit(Collider other)
{
collider.isTrigger = false;
}
}
P.S. don't mix 3d and 2d colliders. Code sample for 3d, in 2d it slightly different.

Related

Is there a way to check if any raycast hits the collider?

I have a GameObject with a box collider and a Player script attached to it.
I want a Function Like This (CheckAnyRaycastIntersection()):
using UnityEngine;
public class Player : MonoBehaviour
{
[SerializeField] BoxCollider bxCollider;
void Update()
{
bool i = bxCollider.CheckAnyRaycastIntersection();
print(i); //Prints TRUE if any Raycast intersects with bxCollider
//Otherwise prints FALSE
}
}
This is useful for me when I'm trying to find out if any gun is targeting the Player. So I want the 'Player' script to calculate this.
Check the GIF
I know there are many ways to do it, but can I do it this way?
I don't understand why you want to do this,
If you want to see gameObject colliding with something else, use the spherecast or boxcast.
In this scenario you should explain who is throwing raycast and who should or should not intersect with the ray.
If you can explain more, i can help.

How do I add multiple animators to my character

I have my main character and added a capsule collider 2d so he would take damage when attacked. When the character is attacking, for example his body is out of the collider space and won’t take damage when hit. Please may you help me solve this issue, I’m very new to unity
One way of doing it is having two different hitboxes that does the same thing but with different shapes so they match two different states of your character.
Enable and disable them in a script when calling the attack using:
Public Collider2D attackCollider; //Drag your attack collider here in the inspector
Public Collider2D idleCollider; //Drag your idle collider here in the inspector
void AttackCollider() //call this fucntion to change to attack collider
{
attackCollider.enabled == true;
idleCollider.enabled == false;
}
void IdleCollider() //call this function to go back to normal collider.
{
attackCollider.enabled == false;
idleCollider.enabled == true;
}
If you want to I'm sure you can incorporate these two functions into an already existing attack function if you have one.
In 3D, you could simply make the Collider a child of any of your bones, it would get animated altogether.
But you mentioned Collider2D, so:
If you have a 2D Rig with bones, you can do the same as obove
If you have a Keyframe Animation, you could animate the collider to match your animation

Detecting collisions in unity without actually applying force to the object

Hey so i am making a 2d game that is very similar to flappy bird (you jump when tapping the screen). The point of the game is to catch the green squares coming to you.
The problem i am encountering is that when my player hits the green square it seems to collide with it (it throws the player in some random direction or even stop into place like it hit a wall) even if on collision the green square is being destroyed.
So whenever the player hits the green square prefab it increments the score and destroys the square but sometimes when i jump right before hitting the square it stops like it collided with it insted of just passing right through.
The player prefab has got a rigidbody component and a 2d square collider.
The green square prefab has got only a 2d square collider component attached to it.
I am very new to unity and this is the first game i started developing.
If you need any further information please don't hesitate to ask.
Here is the player's rigidbody and 2d collider settings
Here is the green square's 2d collider settings
Maybe this picture of the gameplay can help. Imagine the blue square is the player and the green squares are the points you have to hit the red things are just the enemies that kill you so don't mind them. The player is standing still and the green square prefab have a script attached to it that adds a constant force to them. Hopefully this made it a lot clearer.
There is a really useful unity function that will detect when a collider touches another collider.
It is: void OnCollisionEnter(...)
You also have to pass in a Collider or Collider2d. So it will look like:
void OnCollisionEnter(Collider col)
or void OnCollisionEnter(Collider2D col)
Of course you have to have the necessary import statements for unity engine at the top as well.
You may want to use a trigger on your green Squares. then the bird is allowed to pass true but the square will activate your OnTriggerEnter code in your script.
so what you can try is:
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
//Run your code here!!!
//for example
Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
rb.AddForce(new Vector3(1, 1, 1));
}
}
this will add a force to your player/bird. but you can run any code in that method.
OnTriggerEnter and OnCollisionEnter run both when the object is hit by a rigidbody. but with OntriggerEnter you can pass thru the object. because it does not get effected by other physics objects. but onCollision usually does unless you freeze it in position.
side note OnTriggerEnter runs in FixedUpdate, so it is run at the end of each frame

Can You Make A Certain Instantiated Clone Of A Prefab In Unity Get Destroyed When Something Enters Its Trigger?

Basically, I have a bullet that gets shot from a gun on your player when you left-click. I can't specifically destroy the bullet that hits something, it destroys all of the bullets. :C
I Assume A Good Fix For This Would Be To Make It Destroy Itself When Something Enters Its Trigger?
I Don't Know How To Do That Though So If Anyone Can Help That Would Be Awesome!
If You Want To See My Code Then You Can Just Ask.
this is on the thing that the bullet hits:
void OnTriggerEnter()
{
enemyHealth -= 2f;
ishit = true;
}
void OnTriggerExit()
{
ishit = false;
}
its setting a static variable to true and false.
this is on the bullet:
void Update()
{
transform.Translate(Vector2.right * mspeed * Time.deltaTime);
bool hit = Enemy.ishit;
if (hit == true)
{
Object.Destroy(gameObject);
}
}
its using the static bool to destroy itself
Thanks!
Your best bet is calling Object.Destroy on your bullet instance.
That will not destroy any other bullet.
If you mean to destroy the object associated with the current script, you can also call Object.Destroy on this.gameObject.
You can do this call OnCollisionEnter.
Edit: Your problem is not that Object.Destroy is destroying all bullets, but rather that every bullet destroys itself when one bullet hits.
You might want to try:
void OnTriggerEnter(Collider bullet)
{
enemyHealth -= 2f;
Object.Destroy(bullet.gameObject);
}
You can have a script on "damageable" objects tracking collision and destroying the bullet that colides with it, or have the script on the bullet prefab destroying it self on collision.
Deppending on your game an alternative approach is to not instantiate bullets at all, if you are shooting bullets that move as fast as bullets really move, the player isnt going to see them anyway, you can use raycast to see if you are hitting something when the player shoots, make a sound, a muzzle flash, drop an empty cartdrige on the ground, and in case the player hits anything, instantiate some particles or whatever effect the bullet would have on the other end and be done with it...

detecting collisions and destroying gameObjects in Unity

So I'm attempting to create a 2D game in Unity where the gameObjects would be destroyed upon hitting the walls. So i decided to use the isTrigger function and add the script to each of the walls. However, I do not know why the gameObjects don't get destroyed. I have tried to set Collision type as both discrete and continuous for the walls and gameobjects and I've also added static rigidbodys to the walls to see if that would help and even tried changing the size of the collisionbodys of the walls.
Here is the code for the wallscript
public class wallScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTrigger2D(Collider2D other)
{
if (other.tag == "player01")
{
Destroy(other.gameObject);
}
}}
p.s. Even if I remove the conditional statement Destroy() still does not work
You should use void OnCollisionEnter2D(Collision2D other) {...} instead of void OnTrigger2D(Collider2D other).
And uncheck Is Trigger checkbox on your object's Collider.
To Fix The Problem You Are Facing, First Select The Wall Sprite In The Scene, Scroll Down To The Collider And Make Sure Is Trigger Is Checked.
Other Than That Just Check If The Tag You Typed Into The Code Matches The Player You Are Trying To Destroy. Remember, It's Case Sensitive!
After That The Code Should Run Just Fine.

Categories

Resources