- Home /
how to not make both objects collide at once
I have a game where i want to make if i place the 1st object on the second object it goes back to it's original place, but whenever i try to place it on the second object they both go back to their old places here is the 1st object code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerUndoer : MonoBehaviour
{
public GameObject GO;
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Bounce")
{
GO.transform.position = new Vector3(-5.8f, -3.85f, 0);
}
}
}
here's the 2nd object code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerUndoer2 : MonoBehaviour
{
public GameObject GO2;
void OnCollisionEnter(Collision collision)
{
if (collision.collider.tag == "Bounce")
{
GO2.transform.position = new Vector3(-4, -3.85f, 0);
}
}
}
Answer by KevRev · May 01, 2019 at 11:57 AM
It looks like you just need to add a bool to switch who's turn it is.
On a script, have a public bool named Turn (or anything you want really).
On one of the scripts, only run the collider check if it's true, the other script only check if it's false.
Then have an event change Turn true/false depending on how you are determining which piece takes precedence.
I don't know how to say the question right. I have 2 or more gameobjects that when object1 collides with object2, object1 goes back to its place. if object2 collides with object1, object2 goes back to it place. and it's pieces that i lift with mouse. so On$$anonymous$$ouseUp the objects collides. and it's completely random which object i choose to move first.
Yes I think the logic I suggested should still work.
When you select an object with the mouse, ins$$anonymous$$d of a bool, use a temporary GameObject reference.
So something like this:.
public GameObject master;
// In your mouse selection / onDrag section
master=selectedGameObject;
// In your collision section
if (!collision.gameobject=master)
{
[reset object position]
}
Not sure if this will help, but checking if object 1 is moving and if it is colliding with object 2, while checking if object 2 is moving. Or you can set a bool for when mouse is down on Object 1 and then if the two object collide and Object1$$anonymous$$ouseDown is true set object 1 back. And do the same on object 2, I think that is what $$anonymous$$evRev was saying.
code might look like this:
on collision{
if (bool1){ set Go to original position } if (bool2){ set go2 to original position.
} }
you will have to write the actual code. hope this helped :)
alright, i figured it out... i made a bool which was set to false. onmousedown it would be true... and onmouseup i made an ienumerator to wait .001 seconds to turn the bool back to false. and the object going back i made an if statement if bool == true ... then move object... Thanks guys for your help!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Locations not registering as equal 1 Answer
Object reference not set to an instance of an object 2 Answers
Camera bounce back? 0 Answers