I am writing a script that shows a collision between one object and an obstacle. The code runs but does not output within the console. Is there something that I am doing wrong?
using UnityEngine;
public class collision : MonoBehaviour{
void OnConllisionEnter (Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("We Hit an obstacle!");
}
}
}
I added a tag to the object as I will be adding more obstacles to simplify the process. I checked for semicolons and any other errors that would stand out to me. I am not sure what I am supposed to change or if I am missing something.
You actually have a typo in the methods name
// Here
// |
// v
void OnConllisionEnter (Collision collisionInfo)
The code should be
using UnityEngine;
public class collision : MonoBehaviour{
void OnCollisionEnter (Collision collisionInfo)
{
if(collisionInfo.collider.tag == "Obstacle")
{
Debug.Log("We Hit an obstacle!");
}
}
}
When using OnConllisionEnter, check your collider component to make sure the [Is Trigger] flag isn't checked
Collider Component, Is Trigger Flag
Also, remember that both objects have to have a collider component attached to them and only one of them will need a rigid body component attached to it (both of which have to match the correct world space, i.e. 2D/3D RigidBody and Collider.
More about Collisions:
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
Related
I checked nearly every answer for this, but those were mostly simple errors and mistakes.
My problem is that OnCollisionEnter is not called even when colliding whith other rigidbody.
here is the part what does not get called:
void OnCollisionEnter(UnityEngine.Collision col) {
Debug.Log("collision!!!");
foreach(ContactPoint contact in col.contacts) {
//checking the individual collisions
if(contact.Equals(this.target))
{
if(!attacking) {
Debug.Log("hitting target");
} else {
Debug.Log("dying");
//engage death sequence
}
}
}
}
Not even the "collision!!!" message appears. Do I understand the usage wrong, or did I forget something?
Are you using 2D colliders and rigidbodies? If so use this function instead of OnCollisionEnter
void OnCollisionEnter2D(Collision2D coll)
{
Debug.Log(coll.gameObject.tag);
}
You need to make sure that the collision matrix (Edit->Project Settings->Physics) does not exclude collisions between the layers that your objects belong to.
Unity Docs
You also need to make sure that the other object has : collider, rigidbody and that the object itself or either of these components are not disabled.
Try this
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnCollisionEnter(Collision collision) {
foreach (ContactPoint contact in collision.contacts) {
Debug.DrawRay(contact.point, contact.normal, Color.white);
}
if (collision.relativeVelocity.magnitude > 2){
audio.Play();
}
}
}
Here is what I do:
Make sure that object you wish to collide with target has non-kinematic rigidbody and mesh collider. My hitter object is a cube and just change its collider to mesh collider
On mesh colider inspector make sure you enable convex. Please see more mesh collider inspector detail here
Now your OnCollisionEnter works. I hope this helps you.
because you misstyped class name of parameter. this makes no error also not works.
eg:
OnCollisionEnter(Collider other) //this is wrong
OnCollisionEnter(Collision other) //this is correct
You just need attach script to the same object, whose need detects the collision.
I am in the middle of creating a simple 3d platformer in unity and i'm trying to make it so if you are in a certain radius and you hit the "f" key the enemy will disappear. the way i'm checking to make sure the enemy is close enough to be attacked is to have trigger sphere and when its triggered and you press "f" the enemy will be removed. i'm currently trying to see what collided with the trigger but i cant figure it out. here is my current code
using UnityEngine;
public class PlayerCombat : MonoBehaviour
{
public GameObject Enemy1;
public GameObject Enemy2;
public GameObject Enemy3;
public GameObject Enemy4;
// Update is called once per frame
void OnTriggerEnter (Trigger triggerInfo)
{
if (triggerInfo.collider.tag == "Enemy" & Input.GetKey("f"))
{
if (triggerInfo.collider.name == Enemy)
{
Enemy1.SetActive(false);
}
}
}
}
The signature is and has always been OnTriggerEnter(Collider) otherwise that message method will not be recognized by Unity and not get called at all!
And then you already have the according Collider ... what else do you need?
public class PlayerCombat : MonoBehaviour
{
// There is no need to know the enemy references beforehand at all
// you will get all required references from the Collider parameter of OnTriggerEnter itself
// I personally would however expose these two settings to the Inspector to be more flexible
// this way you can adjust these two settings within Unity without having to touch your code
public string listenToTag = "Enemy";
public KeyCode listenToKey = KeyCode.F;
// The signature has to be this otherwise the message method is never invoked at all
private void OnTriggerEnter (Collider other)
{
// Rather use "CompareTag" instead of `==` since the latter will silently fail
// for typos and nonexistent tags while "CompareTag" shows an error which is good for your debugging
// And in general you want to use the logical "&&" instead of the bitwise operator "&" for bools
if (other.CompareTag(listenToTag) && Input.GetKey(listenToKey))
{
// simply set the object you collide with inactive
other.gameObject.SetActive(false);
}
}
}
Finally just make sure that all your enemy instances actually have the tag Enemy
First, I'm pretty sure OnTriggerEnter takes a Collider as its parameter, no? https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
You can get the name of the collided object like so
//Upon collision with another GameObject,
private void OnTriggerEnter(Collider other)
{
Debug.Log($"collided with {other.gameObject.name}");
//you can check for specific components too
MyComponent myComponent = other.GetComponent<MyComponent>();
if (myComponent != null) {
// do something if it has that component on it!
}
}
This is the top of the script:
using UnityEngine;
using System.Collections;
using System.Reflection;
public class DetectPlayer : MonoBehaviour {
GameObject target;
int counter = 0;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Platform")
{
Debug.Log("Touching Platform");
}
}
I'm using a debugger and a break point and it does getting to the line
if (collision.gameObject.name == "Platform")
And on the property name of the gameObject i see: "ThirdPersonController" but it's never get in to the Debug.Log
The script is attached to the Platform like it show in the screenshot. I'm running the game move the player to the Platform when it touch it it stop on the break point but never enter to the Debug.Log
collision.gameObject.name will be called on the gameobject which your collider is attached to so if your collider is not attached to the game object which is named platform , then it wouldn`t be called.
I think #MiladQasemi is right, but I'll try explaining it another way.
The problem as I see it, is your script is attached to your Platform object, and therefore the code if (collision.gameObject.name == "Platform") will never be true. Because the script and the platform are one, an object cannot collide with itself.
Change the code to be:
if (collision.gameObject.name == "ThirdPersonController")
How can I add the game object from above (Roed Knap, Hand etc.) into the script given below (at the picture)?
This is an example project. I can't figure out to reference GameObject's and Colliders into a Script.
What I want to do is very simple.
Make a GameObject with a Collider, and trigger something when collision happens.
So What I have is basically a GameObject Cube that has a Collider sphere added and for this isTrigger is selected. I want this trigger when entering, modify the text. Can you help me with initializing, referencing and other stuffs necessary, see below code. This is the code I work with.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
GameObject Cube;
GUIText Text;
Collider collision;
// Use this for initialization
void Start () {
collision = Cube.GetComponent<Collider> ();
Text = GetComponent ("GuiText") as GUIText;
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter(){
Text.text = "Won"
}
}
Unity uses C# syntax for all of MonoBehaviour and engine namings. That is, a method starts with cap letter:
void OnTriggerEnter(Collider col){}
using UnityEngine;
using System.Collections;
public class chicken_for : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void FixedUpdate () {
if (collision.gameObject.Quad == collisionObject){
Application.LoadLevel("SciFi Level");
}
}
}
I am attempting to when A person touches a quad, he goes to this sci-fi fortress. It however says the name 'collision' does not exist in the current context.
You're referencing a variable (collision) that doesn't exist. You've got a variable collisionObject that doesn't exist, too.
collision is typically the name of the argument to the OnCollisionEnter method. Your code should probably be inside this method instead of FixedUpdate. I'm guessing that you've copied code from a tutorial somewhere but put it in the wrong method.
collisionObject on the other hand is harder to guess, but I expect that if your script is intended to be a component on the player object, then collisionObject should be your quad; if the script is on the quad then collisionObject should be the player.
Either way, you need to declare that variable - probably as a public field so that you can populate it from the inspector.
Add a new function and make sure the player is tagged as player in the inspector. You also need to make sure that there is a collider on the quad that the player touches and a rigidbody component on the player.
using UnityEngine;
using System.Collections;
public class chicken_for : MonoBehaviour {
//This function will handle the collision on your object
void OnCollisionEnter (Collider col){
if(col.gameobject.tag == "Player"){ //if colliding object if tagged player
Application.LoadLevel("SciFi Level"); //load the sci-fi level
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
This should get you roughly where you want to be!