Check which collider trigger in Unity3D - c#

Context
I'm developing an AbstractAliveEntity, which have basically three functions:
Sight: Detect interactable objects. (Collider + Raycast)
Detection: Detect anothers AbstractAliveEntities (Collider + Raycast)
Hearing: Hear noises (Collider)
Currently i'm creating via script empty gameObjects with these colliders.
What i want
I want to know which collider was trigger in OnTriggerEnter and OnTriggerExit
Code
Creating the Sphere collider
private void Start() {
// Creating empty gameObject
sightGameObject = new GameObject("Sight");
sightGameObject.transform.parent = transform;
sightGameObject.transform.localPosition = new Vector3(0, 0, 0);
// Add Component Sphere
_sightInteractable = sightGameObject.AddComponent<SphereCollider>();
// _sightInteractable = gameObject.AddComponent<SphereCollider>();
_sightInteractable.radius = radiusInteractableDetection;
_sightInteractable.isTrigger = true;
}
Detecting
private void OnTriggerEnter(Collider other) {
// How can i detect which collider was?? this.gameObject = "Player" (i want "Sight")
}

Since Unity is originally designed around a component based approach my approach would be to split up the three separate "detection" systems into separate GameObjects with their own collider and script.
AliveEntity
SightController
ColliderA
DetectionController
ColliderB
HearingController
ColliderC
Then you can use the OnTrigger in each separate script to fire a notification to the main AbstractAliveEntity which then handles them on a case by case basis.
Main Script
OnSight(Collider other) {
// Do sight related actions here
}
OnDetection(Collider other) {
// Do detetction related actions here
}
OnHearing(Collider other) {
// Do hearing related actions here
}
Then for each respetive detector:
// reference to master script
public AbstractAliveEntity aae;
OnTriggerEnter(Collider other) {
// replace function with whatever you want to use
aae.OnSight(other)
}
The Added advantage here is that you are now also free to design 'blind' or 'deaf' Entities without all too much hassle. (You simply do not add the respective components)

other.gameObject to get the gameObject the other collider is attached to.
Check unity documentation about collider here for more info.

Related

Ranged attack in Unity3D

I have two objects: a player and an enemy. The main problem is the enemy, which should start shooting at the player when it approaches him at a certain distance (in short, just start the animation).
At first I tried to implement this through Vector 3, as with other ordinary opponents. But he is as stupid and crutch as possible, however, you yourself can see everything in the pinned.
I started to implement it more allegedly correctly, through the trigger and the collider (box collider) of the enemy itself. And everything works right as it should, but there is a nuance. The enemy also has boxing implemented through the box collider, the player, hitting which, causes damage to him. There is only one box collider for these two tasks, and since I had to increase this collider so that the enemy could stop in front of the player at a certain distance. Because of this, the player can hit at a great distance (at which the enemy can shoot) from the enemy and the enemy takes damage anyway.
I tried to make a separate object the size of the enemy himself and use it as a box to receive damage. Then he already transmits data about receiving damage to the enemy object. But this does not work, all links between scripts and objects are made, but he does not want to transfer data. That is, simply making two colliders for different tasks does not work.
In general, here my powers are all. I searched the entire Internet, but I did not find how to implement this mechanic in a different way so that it does not conflict with others. Therefore, I ask the help of the local experts, where I screwed up.
private Animator ch_animator;
// Start is called before the first frame update
void Start()
{
myAgent = GetComponent<NavMeshAgent>();
myAnim = GetComponent<Animator>();
EnemyH = GetComponent<GDHealth>();
}
// Update is called once per frame
void Update()
{
dist = Vector3.Distance(/*checker.*/transform.position, target.transform.position);
if (dist > range)
{
myAgent.enabled = false;
myAnim.SetBool("Idle", true);
myAnim.SetBool("Move", false);
myAnim.SetBool("Attack", false);
}
if (dist <= range & dist > atRange)
{
myAgent.enabled = true;
myAgent.SetDestination(target.position);
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", true);
myAnim.SetBool("Attack", false);
}
if (dist <= atRange)
{
StartCoroutine(Attack());
}
if (PlayerH._health <= 0)
{
atRange = 0;
}
if (EnemyH._health < 0)
{
myAgent.enabled = false;
}
}
public IEnumerator Attack()
{
yield return new WaitForSeconds(0.5f);
myAgent.enabled = false;
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", false);
myAnim.SetBool("Attack", true);
}
void OnTriggerStay(Collider col)
{
if (col.tag == "Player")
{
//gameObject.GetComponent<Animator>().SetBool("Attack", true);
StartCoroutine(Attack());
transform.LookAt(col.transform.position);
transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0);
}
}
void OnTriggerExit(Collider col)
{
if (col.tag == "Player")
{
myAgent.enabled = true;
myAgent.SetDestination(target.position);
myAnim.SetBool("Idle", false);
myAnim.SetBool("Move", true);
myAnim.SetBool("Attack", false);
//gameObject.GetComponent<Animator>().SetBool("Attack", false);
}
}
But this does not work, all links between scripts and objects are made, but he does not want to transfer data.
From this description it could be anything: wrong layers, no rigidbody on either of objects, misstyped tags in OnTriggerStay method.
In my project, I successfully created 2 colliders for 2 separate tasks, so this is how I would see it in your problem:
Use two colliders
Attach one collider with one script to the enemy object - this collider should have a size of the enemy. The OnTriggerStay method here should deal damage to the enemy, check for death, etc.
Create child object to the enemy. Attach new collider to it with the size of enemy's attack range. Attach a script with OnTriggerStay method that will stop enemy and begin ranged attack (or whatever you want to do).
If this doesn't work: check collision matrix or try adding a kinematic rigidbody to either of objects.
Measure distance between player and the enemy in update (which you are already doing) and apply necessary code based on distance (stop or attack) thus replacing one of the colliders.
Hope that helps!

OntriggerEnter not called playerController

I have a script that is a attached to a box, the box has two colliders on for physics and one (slightly bigger) for detecting OntriggerEnters when my player walks over it. I have a script attached to the box that does the following:
public class ColorChangeCollision : MonoBehaviour {
private GameObject Element;
private Color32 BaseColor, CollisionColor;
private Material ElementMaterial;
void Start () {
Element = this.gameObject;
BaseColor = new Color32(255,255,255,255);
CollisionColor = new Color32((byte)Random.Range(0, 255),(byte)Random.Range(0, 255),(byte)Random.Range(0, 255), 255);
ElementMaterial = Element.gameObject.GetComponent<Renderer>().material;
}
private void OnControllerColliderHit(ControllerColliderHit other)
{
Debug.Log("collision...");
ElementMaterial.color = CollisionColor;
}
private void OnTriggerEnter(Collider other)
{
Debug.Log("enter");
}
private void OnTriggerStay(Collider other)
{
Debug.Log("staying..");
}
private void OnTriggerExit(Collider other)
{
Debug.Log("left...");
ElementMaterial.color = BaseColor;
}
}
Main problem OnTriggerEnter or OnControllerColliderHit is never called nor are the other trigger events..
See below for an image of the setup of the box, and its components:
See here for my player who should be calling the OntriggerEnter or OnControllerColliderHit function of the box:
EDIT I modified all the elements to the suggestions of #Programmer. But the OnControllerColliderHit event is still not being called.. (note this function is attachted on the box )
There are 3 issues on how you setup your scene and your code:
1. You are missing a Rigidbody. This should be attached to the "Cube" GameObject since it's not ok to attach Rigidbody to GameObject with a CharacterController.
2. You have two BoxColliders attached to one GameObject ("Cube"). Do not do this. This can cause many issues including a callback function being called multiple times or non at-all.
What to do when you need both trigger and non trigger collider?
Create an empty child GameObject put the trigger there. You can do that.
3. You are using CharacterController so you should remove the OnTriggerXXX callback functions. When using CharacterController, it recommended to use it's callback function OnControllerColliderHit.
void OnControllerColliderHit(ControllerColliderHit hit)
{
Rigidbody body = hit.collider.attachedRigidbody;
......
}
4. CharacterController is not mean to be used as triggers. They are used to detect collisions.
You have two options to make it detect triggers:
A. Remove the isTrigger from the "Cube" Object
Or
B. Add a child empty GameObject under the CharacterController then add the CapsuleCollider or any other collider to it. You don't have to mark it as isTrigger since the "Cube" is already marked as so. You also shouldn't have Rigidbody attached to this child collider.
Create a new layer named "CC" and make the CharacterController and the new child GameObject to be in the "CC" layer. Go to your physics collision matrix settings and make it so that "CC" layer cannot collide with "CC" layer. This will make sure that the CharacterController doesn't detect collsion from tis child object.
You can now use your OnTriggerXXX callback functions to detect collision between the CharacterController and the "Cube" object.
One of them needs to have rigidbody to detect collisions.

Delete spawning list objects consistently

Already 2 days I'm trying to solve this problem but I can't.I'm trying to delete the unlimited spawning barriers consistently when they are collide with the boundary.The barriers are made from 5 cubes.Any ideas?
public List<GameObject> spawning=new List<GameObject>();
public Vector3[] positions = new Vector3[5];
public GameObject barrier;
public GameObject boundary;
void Start()
{
StartCoroutine (SpawnBarrier());
}
void Update()
{
if(true)
{
foreach (GameObject move in spawning)
move.transform.Translate (0f, 0f, -0.1f);
}
}
IEnumerator SpawnBarrier(){
yield return new WaitForSeconds (3f);
while (true) {
for(int i=0;i<=4;i++)
{
spawning.Add (Instantiate (barrier, positions [i], Quaternion.identity)as GameObject);
}
yield return new WaitForSeconds (3f);
}
}
Your barriers are marked as isTrigger on their colliders therefore you can use OnTriggerEnter to detect when any of the barriers collides with the boundary.
You need to create new script, lets call that BoundaryDetector and attach it to the barrier prefab so that every instance of the barrier will have this script attached to it.
When OnTriggerEnter is called, check if the trigger is made by the boundary. This can be done by checking for the "Player" tag since boundary is tagged as Player in your screenshot. If the detected trigger tag is "Player", first remove the spawning from the spawning List then Destroy it.
The BoundaryDetector script is as below (Must be attached to the barrier prefab):
ScriptFromYourQuestion yourInstancingSript;
void Start()
{
GameObject obj = GameObject.Find("NameOfObjectScriptInYourQuestionIsAttachedTo");
yourInstancingSript = obj.GetComponent<ScriptFromYourQuestion>();
}
void OnTriggerEnter(Collider other)
{
//Detect if we collided with the boundary
if (other.CompareTag("Player"))
{
//Remove Self/barrier from the List
yourInstancingSript.spawning.Remove(this.gameObject);
//Delete Self/barrier
Destroy(this.gameObject);
}
}
Note: The ScriptFromYourQuestion should be replaced with the name of the script in your question.
MUST DO:
To get the code above working, the following changes must be made in the Update function of the script from your question:
1.You must attach Rigidbody to at-least one of the Objects (boundary or barrier). In this case, I suggest you do so to the barrier prefab.
2.You must remove move.transform.Translate (0f, 0f, -0.1f); and replace it with Rigidbody.MovePosition and use that to move your barries because you have now attached Rigidbody to your barries and this is the proper way to move a Rigidbody Object.
Maybe something like this:
Your Update function in the code form your quesion should looks something like this:
public float speed = 100.0f;
void Update()
{
if (true)
{
foreach (GameObject move in spawning)
{
//Get Rigidbody component
Rigidbody rb = move.GetComponent<Rigidbody>();
//Calculate Z-axis pos to move to
Vector3 pos = new Vector3(0, 0, 1);
pos = pos.normalized * speed * Time.deltaTime;
//Move with Rigidbody
rb.MovePosition(rb.transform.position + pos);
}
}
}
Your problem is that you have an infinite loop while true that you're not breaking out of. Instead you should add a counter. You haven't explained what you want to do but my example is that it will breakout once it completes 10 instantiations of 5 blocks.
IEnumerator SpawnBarrier(){
spawnCount=0
yield return new WaitForSeconds (3f);
while (true) {
for(int i=0;i<=4;i++)
{
spawning.Add (Instantiate (barrier, positions [i], Quaternion.identity)as GameObject);
}
if (++spawnCount==10)
{
break;
}
yield return new WaitForSeconds (3f);
}
}
If I understood corrently, you want to destroy barriers that collided with some kind of boundary?
If the 'boundary' is the visible screen space
In this case, we can assume that, once the object is invisible to MainCamera it can be counted as out of the boundary. Keeping that in mind, we can use OnBecameInvisible() method, which is method (or message, as stated in Unity Scripting API) called by MonoBehaviour class:
// A piece of barrier that gets destroyed once out of screen
public sealed class BarrierPiece : MonoBehaviour
{
// Called once the object is no longer visible to any (scene editor included) camera
private void OnBecameInvisible()
{
Destroy(gameObject);
}
}
NOTE: if you have more than one Camera in your scene, the object must be invisible to all of the cameras in order for the OnBecameInvisible() to be called.
If the 'boundary' is other object
In this case, there are many approaches based on your game:
Create GameObject with Rigidbody component and add a script which would have OnTriggerEnter(...) method and destroy colliding barriers in it.
(Inefficient) have two static Vector2 variables, one for pivotPosition, other for maximumOffset (this one should be squared for faster calculations). Then, have script on each of the barriers and in Update() checks whether the object's squared distance to the pivotPosition is greater than maximumOffset and call Destroy(gameObject) if the condition is true.
(others) ...
Those are two that came up quickly in my mind.
Hope this helps!
EDIT
Didn't look at the images...
So for your solution, you would need to assign Rigidbody component to the Boundary and add BoxCollider component on all of the barriers. Now, create a tag, called Barrier and tag all the barriers with it. Create a script:
// Destroys barriers on collision
public sealed class BarrierBoundary : MonoBehaviour
{
// Called once the script is created
// Checks if the object has Rigidbody component attached
private void Awake()
{
Debug.Assert(GetComponent<Rigidbody>() != null);
}
private void OnCollisionEnter(Collision collision)
{
if(collision.collider.tag == "Barrier")
Destroy(collision.collider.gameObject);
}
}
(The code wasn't tested, so it can have some typos.)
Now, assign the script to the Boundary. And voila! Barriers are destroyed once hitting the boundary.
Hope (again) this helps!

Unity. Disable collision physics but have method for collision called

Basically, i have player object and bunch of other. For this moment they all have RigitBody2D, and colliders, code for creating:
obj = new GameObject();
obj.tag = "web";
obj.name = "web";
float randomX = Random.Range (Utilities.getMainCameraBounds ().min.x, Utilities.getMainCameraBounds ().max.x);
float randomY = Random.Range (Utilities.getMainCameraBounds ().min.y, Utilities.getMainCameraBounds ().max.y);
obj.transform.position = new Vector3(randomX, randomY);
obj.transform.localScale = new Vector3 (3.0f, 3.0f, 3.0f);
collider = obj.AddComponent <BoxCollider2D>();
body = obj.AddComponent<Rigidbody2D> ();
body.constraints = RigidbodyConstraints2D.FreezeAll;
joint = obj.AddComponent<HingeJoint2D> ();
renderer = obj.AddComponent<SpriteRenderer>();
renderer.sortingOrder = 1;
renderer.sprite = Resources.Load("Textures/dot_net", typeof(Sprite)) as Sprite;
Purpose for this is to catch collision like this :
void OnCollisionEnter2D(Collision2D collision) {
if (collision.transform.tag == "web") {
Player.getInstance().joint(collision.rigidbody);
}
}
After player collides with some of those objects, method joint called, it's purpose to connect player and web with HingeJoint2D to simulate pendulum behvior. Code of joint:
public void joint(Rigidbody2D body) {
WebCollection.getInstance ().turnOffColliders (body.gameObject);
HingeJoint2D hingeJoint = webLine.getObject ().AddComponent<HingeJoint2D> ();
hingeJoint.connectedBody = body;
hingeJoint.anchor = new Vector2 (0.00120592f, 0.1921938f);
hingeJoint.connectedAnchor = new Vector2 (0.1939605f, 0.03025406f);
}
Pendulum behavior is working just fine, but in moment of it, or just when player is moving towards web, other web objects are colliding with it, creating real colliding behavior(like crushing into each other)
My goal: i want web objects to react on the collision(calling method above), but on the screen to player just go throw them.
What i've already tried: I wanted to turnOff other objects bodies (WebCollection.getInstance ().turnOffColliders (body.gameObject);) and after pendulum movement is over, to turn them back, but it causes very strange behavior.
What also can be a solution is to manually checking if player is colliding with web object right now, and handle it, but this is to costly i think.
To put my comment into an answer for those finding your question:
If you want to have Colliders that only trigger events and do not physically collide with other Colliders, then what you want are Triggers. To make a Collider a trigger you check the Is Trigger checkbox of the collider component in the inspector (you can also set it via script with the Collider.IsTrigger boolean property).
To do something when the trigger fires, use the OnTrigger events (OnTriggerEnter, OnTriggerExit, OnTriggerStay) (see http://docs.unity3d.com/ScriptReference/Collider.html)
There is also a short introduction to Colliders as Triggers by unity: http://unity3d.com/learn/tutorials/topics/physics/colliders-triggers

Unity3D - unable to respawn an object after it has been destroyed

I am having an issue respawning a prefab after it has been destroyed. I can't seem to get it to respawn back at its original start position after a second of being destroyed. I have created an empty game object and attached the SpawnTargets.cs script to it. I'm not sure of what the best methodology to approach this situation. Another object with a script attached to it does the actual destroy of the prefab. BulletCollisionHandler.cs works fine though. Thanks for any help. Code is below:
SpawnTargets.cs:
using UnityEngine;
using System.Collections;
public class SpawnTargets : MonoBehaviour
{
public GameObject targetCircle;
public GameObject targetSquare;
public GameObject targetStar;
private Vector3 circleSpawnPosition = new Vector3(0.0f, 1.227389f, -7.5f);
private Vector3 squareSpawnPosition = new Vector3(0.0f, 1.027975f, -7.993299f);
private Vector3 starSpawnPosition = new Vector3(0.0f, 1.8f, -7f);
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
SpawnTarget ();
}
void SpawnTarget()
{
}
}
BulletCollisionHandler.cs:
using UnityEngine;
using System.Collections;
public class BulletCollisionHandler : MonoBehaviour
{
public GameObject targetCircle;
// Use this for initialization
void Start ()
{
Destroy (gameObject, 2);
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter(Collision other)
{
if(other.gameObject.name == "TargetSquare")
{
other.gameObject.rigidbody.isKinematic = false;
((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false;
Destroy (other.gameObject, 1);
Debug.Log("Hit square");
}
else if(other.gameObject.name == "TargetCircle")
{
other.gameObject.rigidbody.isKinematic = false;
((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false;
Destroy (other.gameObject, 1);
Debug.Log("Hit circle");
}
else if(other.gameObject.name == "TargetStar")
{
other.gameObject.rigidbody.isKinematic = false;
((TargetMovementHorizontal)other.gameObject.GetComponent<TargetMovementHorizontal>()).enabled = false;
((TargetMovementVertical)other.gameObject.GetComponent<TargetMovementVertical>()).enabled = false;
Destroy (other.gameObject, 1);
Debug.Log("Hit star");
}
}
}
You're not calling Instantiate() anywhere, so it's hard to see where the new object would come from in the code you've supplied.
In any case, it might be better not to use Destroy. If you want to immediately reset the object, why not simply recycle it back to the start position? It's a good idea to avoid instantiating and destroying lots of objects, it's better to hide/disable the ones your don't need and unhide/re-enable them.
Here's a tutorial on the general idea. The tutorial is about groups of objects but the same trick would work for recycling single objects too.
You are better of using gameObject.SetActive( true/false ); for activating / deactivating the gameObject instead of just using Destroy.
Then if you are using Destroy you have 3 options that comes to mind for getting it into the desire position before the Player sees it.
1) You enable the game object after disabling its Renderer component. Then you equalize the transform's position / rotation the one you need. After this you re-enable the Renderer component. It should be placed where you want it.
2) You Instantiate the gameObject, but first making sure the Renderer component is disabled on its Prefab, by default, so you can re-assign its Transform values then - re-enable the Renderer again.
3) You make an invisible gameObject (an Empty gameObject) and Instantiate the wanted gameObject, you then make the Empty to be the parent of the newly created gameObject.. Provided that the parent Empty is exactly where you want it to be, when you instantiate and reset the child's position it should jump off right on top the the Empty parent.
I'm not giving code since you haven't and I don't have no idea of which method you might end up liking more. In terms of performance the Enable/Disable are the best option.
And as theodox says Object Pooling is your best friend for things like bullets, although it might be applied to many other gameObjects that might work as 'collections of objects' on your game's logic. It's totally worth learning.

Categories

Resources