In my code, I use the CircleCast2D method to see if an object has been clicked, if yes, find out what tag it has and then do different stuff based on which tag it is.
The entire .cs file is in this pastebin: spaghetti code (yay!) - Pastebin.com
but the part that has smth to do with the issue is this:
public class PlaceAndDragObjs: MonoBehaviour
{
RaycastHit2D hitA;
RaycastHit2D hitB;
public Transform placedObjParent;
public int selectedObj;
public List<GameObject> placeableObjs;
GameObject objToBeManipulated; // = dragged or rotated
public List<FixedJoint2D> placedJointsToBeDeleted; // These are the joints connecting placed objs with the background
// Must be deleted when construction is over and the vehicle is tested
Quaternion noRotation = Quaternion.Euler(0, 0, 0);
public float timer = 0f; // These are public for debugging purposes
public bool scheduleMouseCheck = false;
public float dragTimeTreshold;
public bool dragMode = false;
RaycastHit2D standardHit; // this is needed so that lines such as 183 and 186 don't generate error CS0165
// Start is called before the first frame update
void Start()
{
standardHit = Physics2D.CircleCast(new Vector2(100, 100), 0.1f, Vector2.zero, Mathf.Infinity, Physics2D.DefaultRaycastLayers, 0f, 0f);
// this is needed so that lines such as 183 and 186 don't generate error CS0165
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
// Check if there is something in the clicked grid-cell:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Vector2 roundedClickPos = new Vector2(0, 0);
roundedClickPos.x = Mathf.Round(ray.origin.x);
roundedClickPos.y = Mathf.Round(ray.origin.y);
hitA = Physics2D.CircleCast(roundedClickPos, 0.5f, Vector2.zero, Mathf.Infinity, Physics2D.DefaultRaycastLayers, 1f, 1f); // boxes and wheels
hitB = Physics2D.CircleCast(roundedClickPos, 0.5f, Vector2.zero, Mathf.Infinity, Physics2D.DefaultRaycastLayers, 0f, 0f); // everything else
if (hitA.collider == null && hitB.collider == null)
{
Debug.Log("empty");
// If there isn't anything there, place the selected object:
Vector3 vec = new Vector3(0, 0, 0);
vec.x = roundedClickPos.x;
vec.y = roundedClickPos.y;
switch (selectedObj)
{
case 1:
case 2:
case 3:
{
vec.z = 1;
}
break;
default:
{
vec.z = 0;
}
break;
}
GameObject placedObj = Instantiate(placeableObjs[selectedObj], vec, Quaternion.Euler(0, 0, 0), placedObjParent);
placedJointsToBeDeleted.Add(placedObj.AddComponent<FixedJoint2D>() as FixedJoint2D);
AdjObjCheck(placedObj);
}
else if (hitA.collider != null && hitB.collider == null)
{
Debug.Log("there is a box there");
Debug.Log(hitA.collider.tag);
objToBeManipulated = hitA.collider.gameObject;
// If there is a box there, schedule a check to see if the mouse is still held down dragTimeTreshold seconds later
timer = 0f;
scheduleMouseCheck = true;
}
else if ((hitA.collider == null && hitB.collider != null) ||
(hitA.collider != null && hitB.collider != null))
{
Debug.Log("there is a non-box there");
Debug.Log(hitB.collider.tag);
objToBeManipulated = hitB.collider.gameObject;
// If there is a non-box there, schedule a check to see if the mouse is still held down dragTimeTreshold seconds later
timer = 0f;
scheduleMouseCheck = true;
}
}
if (timer > dragTimeTreshold && scheduleMouseCheck)
{
scheduleMouseCheck = false;
if (Input.GetMouseButton(0) == false)
{
Debug.Log("short click detected");
switch (objToBeManipulated.tag)
{
case "wheel_wood":
objToBeManipulated.transform.Rotate(new Vector3(0, 0, -90));
break;
default:
break;
}
}
else
{
// A mouse input longer than dragTimeTreshold seconds = start drag -> enter "dragMode"
dragMode = true;
FixedJoint2D[] fixedJoints;
fixedJoints = objToBeManipulated.GetComponents<FixedJoint2D>();
foreach (FixedJoint2D joint in fixedJoints)
{
if (joint.connectedBody != null)
Destroy(joint, 0f);
}
}
}
The basic premise is that you can place objects in a grid. Objects are either on
- position.z = 1 → either a frame or an obj that you cannot put in a frame
- position.z = 0 → an obj that you can put in a frame
If you click an object, it will get rotated, if you hold the mouse button, it will enter “dragMode” and will follow the cursor’s position.
There is a special object, meant to be a wheel.
The object itself has a 2d box collider and 2d rigidbody, and it is also a parent of two other objects:
- one is just a sprite
- the other one is a wheel, connected to the parent object with a 2d hinge joint. It also has a 2d rigidbody
And it is this object, that when clicked, is ignored every other time. None of the other objects do this when clicked.
I really don’t know what is going on.
Any advice - even tips for how to make the rest of the code better/cleaner - is very welcome.