Detecting Collisions in Unity? - unity3d

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

Related

Unity and XR Interaction Toolkit: Cancel a teleport

Unity 2021.3.16f1/URP 12.1.8
Creating an Escape Room-ish game. After they have solved all puzzles, a space will open to which they can teleport. The idea is that if they try to teleport there, instead of actually teleporting, I want them to get some sort of message they have finished the level.
So how can I stop them actually teleporting there? What I've done so far is create a Teleportation Area that only becomes active after all puzzles have been solved. In the "Teleportation Area" component, under "Interactable Events", I've set it up that whenever the "Teleporting" event is fired, my script is called.
So in this script, can I actually cancel the teleport? Or do I need to do it someplace else?
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.UIElements;
public class TeleportExtra : MonoBehaviour {
public void CancelTeleport( TeleportingEventArgs eArgs ) {
// Would like to cancel the teleport here. But how?
}
}

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!

There is no reason for this Unity script to make the sprite move, yet it moves

Here's the code:
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody2D rb;
public float strength = 100f;
void Start () {
//Initialize the body of the sprite so that forces
//can be applied.
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
//Note2Self: var says this is a variable of unspecified type.
var touch = new Touch();
/*
if (touch.phase == TouchPhase.Began){
rb.AddForce(transform.forward * strength);
}*/
if (Input.anyKey)
rb.position.Set(0, 100);
}
}
I was trying to practice some basic stuff in Unity (I am not used to programming in IDE's at all, we've just used vim in my program so far) when I happened upon this oddity.
First, I didn't understand why the sprite would move at all when there can't be a touch identification, since I haven't actually tested this on a mobile device. So I commented it out and for some reason the sprite still moves. That code shouldn't be doing anything, yet it is.
I have checked to see if the sprite is using the up-to-date script - it is - and I have checked if the script is targeting the correct rigid body and that it is a rigidbody2D. It is.
What is going on?
If it is just falling it is probably effected by gravity.
You can turn this off in your script by adding rb.gravityScale = 0; at the end of your Start() function
OR
by setting it in the editor inside the rigid body component
I looked in the unity documentation:
https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
which says that applying a Rigidbody2D component to an object will place it under control of the physics engine.
The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces.
I have run into issues with rigidbodies on more than one occasion, I suggest you check the RigidBody2D component in the unity inspector window and make sure to uncheck use gravity.
Also, you may want to just write a custom script without using a rigidbody. Doing a search on youtube will probably give you exactly what you need for that. Hope this helps!

How to get an OnTriggerEnter2D to recognize objects entering it?

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.

Player dies when hitting the floor

I am trying to make a game in unity, and I am new to unity and coding, and I have started making a game, I have made some progress on it but I am having trouble finding some of my answers on youtube and the unity forum, and sometimes when I do, I still can't get things to work. So this is what I'm trying to do.
I have a map and the player is on top of the tower, I want the player to fall and when hitting the ground, dies with it displaying game over, What could I do to make this happen and what script?
So i have this now,
// Ground.cs: Kills players that touch this.collider.
using UnityEngine;
// Attach this to the grass collider
public class Ground : MonoBehaviour {
// Called when another collider hits the grass.
// This is part of Unity!
void OnCollisionEnter(Collision c) {
// Does the other collider have the tag "Player"?
if (c.gameObject.tag == "Player") {
// Yes it does. Destroy the entire gameObject.
Destroy(c.gameObject);
}
}
}
Now, I need to it transition to a game over overlay, which asks me to restart, yes or no.
The resources are out there in regards to learning Unity3D efficiently.
Look at the Unity3D tutorials: https://unity3d.com/learn/tutorials
These tutorials are usually kept up to date in terms of not using deprecated code. Additionally, several of the tutorials will teach you how to set up events like those that you need.
In regards to your immediate question though, look into forming the logic for your game.
You're going to need allow your player gameobject to fall via either gravitational force enacted on a rigidbody or hard-coded physics being applied to the gameobject. Next you will need to determine if the player gameobject has collided with the "floor". If so you will need to trigger an event to destroy the player gameobject, then display a GUI Text that says Game Over.
Look around more at tutorials and get better acquianted with Unity. Over time, you'll learn how to make things happen. If you have more questions, feel free to ask.
Answer to you update:
If your code is functioning correctly and your destroying your gameobject correctly, then awesome! You're learning fast!
The next step could be:
Create a Canvas, create a gui panel, create a gui text
That gui text object can have your Game Over Text
Now, create a button that you will utilize as the restart button
You can have the button call a function which utilizes SceneManager.LoadScene to reload the scene. Look here for an example: http://answers.unity3d.com/questions/1109497/unity-53-how-to-load-current-level.html
Next, Disable the panel as you will not need it until your player dies.
When your player collides with the ground you will destroy the player gameobject and set the panel you created to active. (you can trigger these actions via a listener in code or via the button component in the inspector).