I'm brand new to Unity3D 5 and I'm having issues with my first 2D game involving collision detection. My moving object is a ball and has Rigidbody2D and CircleCollider2D. My stationary "collider or trigger" is a BoxCollider and has a script attached. The OnTriggerEnter2D should be fired when the ball passes through the stationary box. I've also tried OnCollisionEnter2D but I'm fairly certain I should use OnTriggerEnter2D because my stationary box is marked as a trigger.
My code:
public class LoseCollider : MonoBehaviour {
public LevelManager levelManager;
void OnCollisionEnter2D(Collision2D collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter2D(Collider2D trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
void OnCollisionEnter(Collision collision)
{
print("collide");
levelManager.LoadLevel("Lose");
}
void OnTriggerEnter(Collider trigger)
{
print("trigger");
levelManager.LoadLevel("Lose");
}
}
As you can see I'm testing all variations and none are being called. Here are my Unity properties for my objects:
And
I'm sure I'm missing something simple if anyone can please point it out to me.
If you are going to to use OnTriggerEnter2D, you must also use a 2D Collider. You are currently using BoxCollider as shown in your second screenshot.
Change that to BoxCollider2D and your Collision callback functions should be called.
I know you already solved it, but I ran into the same problem and found your post. I was using the correct 2D collider, I was missing the Ridgedbody 2D component. It took me a while to find that so I wanted to add it here in case someone else comes across the same problem.
Related
I've been trying a way to get rid of an enemy in my game, but once the weapon in the game collides with it it does not do what it's suppose to do. So I tried a different way by turning of the box collider2d, the sprite renderer and another script inside it. But it still does not work.
enter image description here
Just use Destory to destroy the gameObject
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Sword")
{
Destroy(this.gameObject);
}
}
I have visited many forums for this problem and I have searched lots of tutorials for this issue.
Here's my code :
private void OnTriggerEnter(Collider collision)
{
GameObject collisionGameObject = collision.gameObject;
if (collisionGameObject.name == "playerBullet")
{
Debug.Log("damage taken");
TakeDamage(40);
}
}
I should be doing everything right, what went wrong?
Make sure of the following things:
Your GameObject with the script must have a collider.
Your GameObject with the script must be set to Collider and not Trigger
Your GameObject with the script must have a RigidBody
Your Bullet must have a collider.
Your Bullet must have a collider that is not a trigger.
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.
Note: It's a 2D Game
I'm trying to get an audio clip to play when the Character comes into contact with an object that has a Box Collider around it.
I've tried OnTrigger/OnCollision methods but neither are playing any sound. I've also tried many solutions online but still no sound on collision. My Clip works on Awake, but not as intended.
Checklist:
The Player has a "Player" tag.
The Player has a Rigidbody 2D and a Collider 2D
The Oject Collider has 'Is Trigger' ticked
The Object has an AudioSource
The Object has an Audio Script
The Object has a Box Collider 2D for the collision around it
The Audio Clip is attached to the AudioSource component
Here's the current Script (Attached to Object):
I'd be very thankful!
public class Audio : MonoBehaviour
{
public AudioSource audioClip;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
audioClip.Play ();
}
}
}
In order to find out what is wrong, you can start by simplifying the problem.
You can start by doing something simple in OnTriggerEnter2D, that you know will work, such as a Debug.log
void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log(this.gameObject.name+ " collided with "+collision.gameObject.name)
}
This way, you will know if it's the collision or the audio that's the problem.
It may be, that the collision works, but you are unable to hear the audio because of wrong settings or some other minor detail you have missed.
I am trying to make a replica of Asteroids in Unity. The problem is that my bullets are not triggering the OnTriggerEnter2D method on the asteroids. The asteroids have the following script attached:
using UnityEngine;
using System.Collections;
public class Asteroid : MonoBehaviour {
void Start () {
print ("class initiated");
}
void onTriggerEnter2D (Collider2D collider) {
Debug.Log (collider);
}
}
The bullet GameObject has Is Kinematic and Is Trigger enabled, and has Rigidbody 2D and Box Collider 2D attached. The asteroid GameObject has Rigidbody 2D and Circle Collider 2D, and Is Kinematic and Is Trigger is unchecked.
The problem is the Spelling. The o in onTriggerEnter2D should be capitalized. Simple mistake like this one can ruin your day. I didn't even notice it until I ran your code.
void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(collider);
}
Next time if you are not sure about the spelling of the Unity callback function, right click in Visual Studio then click Implement MonoBehaviours search for the function you want, select it and click OK. Visual Studio will add that function for you.