OnCollisionEnter is not working: - unity3d

Please Guys help, I am new to Unity and programming:
I have a two Bouncing Ball, tag as BouncingBall1 and BouncingBall2, I want when a bullet hit both to destroy and if the time have not exceeded displaySecond and you have destroy the balls you win, but my problem is the OnCollisionEnter is not working. The part of wining is not working the rest are, my code fragment is below.
function OnCollisionEnter(col.collision) {
if ((displaySecond < 30) && (
col.gameObject.tag ==
BouncingBall1 == null &&
col.gameObject.tag ==
BouncingBall1 == null)) {
print("You have won");
}
}

col.gameObject.tag == BouncingBall1 == null
I think the problem is with the way you check if an object is destroyed. This isn't the way to do it. Also, you're checking if two identical lines of code are identical. So this will always return true if the display second is lower than 30.
Try changing it to
if ( (displaySecond < 30) &&
(col.gameObject.tag == "BouncingBall1") ) {
Debug.Log("You have won!")
}

It sounds like you want keep track of whether each ball has been destroyed and display "You have won" when both have been destroyed. This code is a little crude but should achieve this:
var ball1Destroyed = false;
var ball2Destroyed = false;
...
function OnCollisionEnter(col : Collision) {
if (col.gameObject.tag == "BouncingBall1") {
ball1Destroyed = true;
}
if (col.gameObject.tag == "BouncingBall2") {
ball2Destroyed = true;
}
if (displaySecond < 30 && ball1Destroyed && ball2Destroyed) {
Debug.Log("You have won");
}
}
This code assumes that the only collisions will be between bullets and balls (not between one ball and another), this can be done with collision layers.

Related

Unity3d - Collision detection and position inaccuracy

I'm a beginner on Unity3D and I decided to create a 3D Tetris to start learning. Everything was fine so far, but I have a problem with collision detection.
https://gyazo.com/49fbf5798dc67a546c5e187fde8f6096
As you can see on this screen, the blue tetromino is not at the good place. This is due to the fact that the orange tetromino position in X is not precise (-2.9999).
Each block that makes up a tetromino has the tag "Block". The ground has the tag "Ground".
This is how I detect collisions in my OnCollisionEnter method.
if (collision.collider.tag == "Block" && !detectedBefore)
{
for (int k = 0; k < collision.contacts.Length; k++)
{
if (Vector3.Angle(collision.contacts[k].normal, validDirection) <= contactThreshold)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
break;
}
}
}
if (collision.collider.tag == "Ground" && !detectedBefore)
{
GetComponent<TetrominoMovement>().Snap();
GetComponent<TetrominoMovement>().enabled = false;
FindObjectOfType<Tetromino>().GenTetromino();
detectedBefore = true;
}
Do you know how to get around this problem? Thank you in advance.

How to get my overlapsphere to work?

This is my error code :
Assets/Scripts/PlayerDamage.cs(166,35): warning CS0219: The variable `hitCol' is assigned but its value is never used
I'm just not sure why it's not working properly. I thought I was using it in the input function under attackVKC. I should mention this is for a multiplayer game in unity.
Basically damage is getting done to the player casting the attack and not to the other players. I'm trying to get an aoe (area of effect) attack.
private Collider[] hitColliders;
else if (Input.GetButton ("Special") && (Input.GetButtonDown ("Fire2") && offcd == true && offccd == true)) {
attackVKC();
StartCoroutine("GlobalCooldown");
StartCoroutine("GlobalCCooldown");
}
[Client]
void attackVKC(){
hitColliders = Physics.OverlapSphere (transform.position, special.rangeVKC, mask);
{
if (GetComponent<Collider>().tag == PLAYER_TAG)
{
CmdPlayerHit(GetComponent<Collider>().name, special.damageVKC);
}
}
Debug.LogError (GetComponent<Collider>().name);
}
I actually sloved this. I wasn't looping through the array and I didn't realise getcomponent was for the gameobject attached. So that's why it was hitting the player using the attack and not other players.

Colliding objects bouncing off when isTrigger is true instead of passing through

I have looped through some game objects to set the isTrigger property before a certain collision occurs but though the property is true collision with the object still occurs. Please see the relevant code below:
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "object1") {
for (int i = 0; i < badGuys.Count; i++) {
badGuys[i].getBadGuyGameObject().GetComponent<Collider2D>().isTrigger = true;
}
}
else if (col.gameObject.tag == "object2") {
// collision with object1 always occurs before object2, though isTrigger is true the colliding object doesn't pass through the badGuys game objects, it bounces off on collision
}
else if (col.gameObject.tag == "object3") {
for (int i = 0; i < badGuys.Count; i++) {
badGuys[i].getBadGuyGameObject().GetComponent<Collider2D>().isTrigger = false;
}
}
else if (col.gameObject.tag == "badGuy") {
}
}
collision with object1 always occurs before object2, though isTrigger is true the colliding object doesn't pass through the badGuys game objects, it bounces off on collision. How can I solve this?
UPDATE 1
I just realized that the isTrigger value is true or false in the inspector depending on the time I stop the game. For example if I stop the game after Object1 collision the isTrigger is true and when I stop the game after collision with object3 it false. This inconsistency makes debugging very cumbersome. Is this a bug or something?
UPDATE 2
Based on recommendation from Joe Blow and Agustin0987 I used the enabled property of Collider2D instead of isTrigger. I also removed virtually all the code in OnCollisionEnter2D to get a simple test scenario. Please see code below:
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "object1") {
if (badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = false) {
badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = true;
Debug.Log ("CHANGED TO TRUE");
Debug.Log ("bad guy collider enabled is " + badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled);
}
else if (badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = true) {
badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled = false;
Debug.Log ("CHANGED TO FALSE");
Debug.Log ("bad guy collider enabled is " + badGuys[4].getBadGuyGameObject().GetComponent<Collider2D>().enabled);
}
}
else if (col.gameObject.tag == "badGuy") {
Debug.Log("collided with bad guy"); // DOESN'T OCCUR
}
}
Instead of looping through this time, I decided to test for one. The else if is where enabled is true always satisfied though the Log prints false and the if where enabled is false is never satisfied. In the inspector, the Box Collider2D for bad guys is always unchecked. Collision with the badGuy never occurs. Based on my simple scenario code I thought it should be working.
Never, ever ever
I mean absolutely never
use "else if".
Simply, never, ever - ever - type "else if" for the rest of your life, until you die.
In the first instance, delete all your code and replace it with this:
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("We will deal with this ......... " +col.gameObject.name);
if (col.gameObject.tag == "object1")
{
Debug.Log("\t handling 'object' type issue");
HandleObjectIssue(col)
return; //NOTE HERE
}
if (col.gameObject.tag == "object1")
{
Debug.Log("\t handling 'bad guy' type issue");
HandleBadGuyIssue(col)
return; //NOTE HERE
}
}
private void HandleObjectIssue(Collision2D col)
{
Debug.Log("'\t\t object', I'm dealing with this .. " +col.gameObject.name);
}
private void HandleBadGuyIssue(Collision2D col)
{
Debug.Log("\t\t 'badguy', I'm dealing with this .. " +col.gameObject.name);
}
Run that extensively and "unit test" it. If you like, convert back (USING THAT CODE) to a "loop" method to look at all items. In any event, test that extensively.
--
Secondly, add this routine ..
private void ToggleColliderOnGameObject( GameObject gg )
{
Debug.Log("I'm supposed to toggle the collider on: " +gg.name);
Collider2D { or whatever } col = gg.GetComponent<Collider2D>();
bool currentState = GetComponent<Collider2D>().enabled;
Debug.Log("\t it is currently: " +currentState);
bool newState = ! currentState;
col.enabled = newState;
Debug.Log("\t\t but now it is: " +newState);
}
... and start unit testing with it. So you will have code like:
ToggleColliderOnGameObject( badGuys[4].getBadGuyGameObject() );
For example, try some things like this:
void Start()
{
InvokeRepeating("teste", 1f, 1f);
}
private void Teste()
{
ToggleColliderOnGameObject( badGuys[4].getBadGuyGameObject() );
}
After experimenting with that for some time and unit testing, you will be able to move on.
Note that apart from other problems, it is likely that (for example) your routine "getBadGuyGameObject" is returning the wrong thing - or some similar problem.
EDIT:
Remove badGuy = badGuyPrefab; from your public void createBadGuy(GameObject badGuyPrefab, BadGuyType badGuyType, Vector3 badGuyPosition) function. This was a mistake in Programmer's code from the link in your comment.
It seems like you are trying to enable/detect the collision between badGuys and object2 only if object1 collides with whatever object has this script attached (seems to be some kind of BadGuyManager). So I would recommend that instead of setting isTrigger to true or false, you should try to enable or disable the whole `Collider2D.
P.S: I agree with Joe Blow in that you should explain what are you trying to achieve.

Unable to destroy gameObject?

I am trying to destroy a gameObject once it has collided with two other objects with the following code but it does not work.
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Sphere" && col.gameObject.tag == "Pyramid")
{
Destroy (GameObject.FindWithTag("Pentagon"));
}
}
Can someone offer a correction of my code because I can't seem to figure out how to destroy my "Pentagon"?
The problem is in your first line:
if (col.gameObject.tag == "Sphere" && col.gameObject.tag == "Pyramid")
You cant have 2 tags on a gameObject. I assume what you want to say is || instead of &&.
What are you trying to achieve in that second block of code? Instead of deleting the rigidbody, maybe just set RigidBody.Enabled = false.

Strange If/Else Behavior

Context
I am making a mobile game in which the player is required to touch objects in a specified order. The correct order is determined in a List called clickOrder. To determine the current object the player is supposed to click, currClickIndex is used.
Problem
When touching a correct object, the debug text will display "Correct" for a split second, and will then immediately change to "Wrong." What I am unsure about is why both the if and else blocks are executed when only touching a single object.
Code
void Update()
{
if (Input.touchCount == 1)
{
if (this.enabled)
{
Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);
if (hit != null && hit.collider != null)
{
// check if the touched object is the correct one
if (hit.collider.gameObject == clickOrder[MyData.currClickIndex])
{
debug.text = "Correct";
MyData.currClickIndex++;
}
else
{
debug.text = "Wrong";
}
}
}
}
}
As soon as the correct object is being touched, you do this:
MyData.currClickIndex++;
which moves you forward in the ordered sequence, and from then on, the previously correct object is not correct anymore. But you're still touching it.
If you want to avoid this, you need to move forward in the sequence after you've touched the correct object.
if (there are touches and the correct object is being touched)
{
set a flag;
}
else if (a flag has been set)
{
MyData.currClickIndex++;
reset the flag;
}