How to get an OnTriggerEnter2D to recognize objects entering it? - unity3d

I am trying to create am endless jumper and I want to use a Cube to detect the Platforms and destroy them.
The problem I am having is that the Triggers aren't being recognized by these cubes.
The green is the destroy Cube.
I have the RigidBody2D and BoxCollider2D on the destroyer.
public class DestroyPlatform : MonoBehaviour {
void OnTiggerEnter2D (Collider2D collider)
{
Debug.Log("I am in the destroy Wall");
}
}
This is the code I am using. As I mentinoed I just want it to tell me it triggering. I have seen tons of code on how to destroy it, but this basic code isn't working.
All of the platforms are 2D Sprites and are using the following:
Notice both GameObjects are triggers. I have also tried to put the script on the platform prefabs and the destroyer cube.
Thanks for the help.

Related

Unity3D - How can I detect collison between my car object and terrain?

I am working on a driving game for a project. My issue is that I'm unable to detect collision (I want to place the car to a checkpoint whenever it goes off the road) between the wheels of my car and the terrain. I tried to use this simple script but it does not seem to work:
using UnityEngine;
public class CarCollision: MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
if (other.collider.name == "Terrain")
{
Debug.Log("We got off the road!");
}
}
}
Is it a 2d game? If yes, use OnCollisionEnter2D instead of OnCollisionEnter.
Is the script attached to the gameobject?
Are you sure that the terrain has a collider which is not marked as trigger?
Try copying and pasting this same thing in the main controller.
I hope it worked. Don't forget to mark my answer as accepted if it worked!

Unity: Drag & Drop script doesn't work on my prefab gameobject but DOES work on regular UI gameobjects

I have a simple script that works just fine on any normal UI objects like an image.
But it doesn't work at all for my prefab gameobject, why is that?
I have attached a whole bunch of components to try out, like images, canvas renderer and all kinds, nothing works for me. I can also only SEE the damn prefab when I run the game, I can't see it on the Game tab. I have no clue. This is so strange.
Anyways, If I didn't make myself clear, please let me know what I need to explain or include for anyone to give me an answer?
public class DragAndDrop : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log("onBegindrag");
}
public void OnDrag(PointerEventData eventData)
{
this.transform.position = eventData.position;
Debug.Log("ondrag");
}
public void OnEndDrag(PointerEventData eventData)
{
Debug.Log("onEndDrag");
}
}
Does this script only work on UI things? If so, Can I add a component to my prefab so that it is a UI item? Im working in 2d.
By default these primerily UI events are only casted onto UI elements (Image, Text, etc) which have Raycast Target enabled (using the GraphicsRaycaster component attached to the according Canvas).
If you want to be able to also use them on "normal" 3D objects you need
An EventSystem in your scene (already the case otherwise it wouldn't work for UI either)
Collider components on your objects
A PhysicsRaycaster component on your Camera!
For some reason I don't know this is only mentioned in the IPointerClickHandler (Unity 2019.1 and older) but afaik this still applies to all the EventSystem-related events / interfaces.

How to make a platformer in Unity?

(I am a new person in stackoverflow, might ask a bit different)
How to make a platformer in Unity?
Unity 2020.2.0a11.1312.3
Info before start: I want to make a mobile game.
(with some buttons)
Hello, I want to make a platformer in Unity.
I tried to use Rigidbody2D.AddForce() method but I couldn't get it working.
Can somebody help me?
I don't usually work on mobile games in Unity but these are generally the reasons why rigidbodies don't work.
Make sure to connect the script to the object with the rigidbody by dragging the script on to the object.
Use RigidBody2D Rigidbody2D = GetComponent<RigidBody2D>() at the start of your script.
If you're using the AddForce() method in the Start() function then you need to move it to the FixedUpdate() function.
Sometimes the movement does work but it doesn't move the object very fast because there isn't enough force so you might have to increase the force.
Check to make sure that there are no movement constraints applied to the Rigidbody.
Since you're working with buttons you will have to link the buttons to the script with the rigidbody like:
public void OnPointerDown(PointerEventData eventData)
{
//Start moving logic
moving = true
}
public void OnPointerUp(PointerEventData eventData)
{
//Stop moving logic
moving = false
}
private void FixedUpdate(){
if(moving){
//Moving
Rigidbody2D.AddForce(force * Time.deltaTime)
}else{
//Not moving
}
}
Since OnPointerUp/Down is only called once when the event happens you need to put your start moving logic in OnPointerDown and stop moving the object in OnPointerUp.

Detecting Collisions in Unity?

I'm making a game in Unity where I want collisions between two moving objects to be detected (One of which is moved by the player using touch. For testing reasons I'm currently writing the script for mouse controls). However for some reason when the game object that is moving moves into the collider field of the object that needs to trigger an event when collided with, nothing happens. I added colliders to both objects and added Is trigger to the collider of the object that needs to trigger the event and as required but it still doesn't work.I tried it with the code that is supposed to trigger the event first and it didn't work and then I simply tried to use debug.log to see if the problem was with the code related to the event I want to get triggered but nothing works. Does anyone know how I might solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Hit Detected");
}
}
You need to put a Rigibody on the 2 objects,
if your project is:
2D: "Gravity Scale" to 0 (so they don't have gravity)
3D: Set checkBox "Use Gravity" to false

Detecting a collision between two colliders

I'm developing a game in unity using UnityScript. I've initially created two objects, a sphere and a cube, having respective colliders. Now I'm trying to detect a collision between them using the below UnityScript function. But I'm not able to detect collision. I've also added a rigid body component to both of them. How can I detect a collision?
function OnCollisionEnter(col : Collision)
{
if(col.collider.tag=="Cube")
{
Debug.Log("collision");
}
Things to verify:
Make sure the script(s) are attached to the game objects
UPDATED: Make sure the class you have this code in inherits from MonoBehaviour (C#, boo)
Make sure collider components are attached to the game objects and that "Is Trigger" isn't checked as OnCollisionEnter won't be fired if it is.
Make sure the gameobject has the "Cube" tag assigned in the inspector
Also, the rigidbody component only adds physics to the objects and is not necessary when simply detecting collisions.