Hi,
I am trying to instantiate a gameobject to a location on mouse click. The mouse click works fine, but the instantiation seems to pick a position other than what I thought would be the correct position. I will use images to help explain this.
So here in the first image, I click the 2 of diamonds card, and the slot highlights. I then click the slot to instantiate the position. The problem I have is, the new object is instantiated on top of the old object instead of the hit.transform.position. The parent seems to hold the new instantiation fine.
My Code:
You can view it in Image format here.
// If we have a counter,
// Place the counter if the user clicked on the highlighted square
if (Counter_Manager.Script.CounterOnMouse == true)
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.up);
if (hit != null && hit.collider != null)
{
// If the slot collider we click is in the same position as the one we cached
// Set the counter to the position of the card slot
if (hit.collider.transform.position.x == Board.Script.TempChosenCardLocation1.x &&
hit.collider.transform.position.y == Board.Script.TempChosenCardLocation1.y)
{
print(hit.collider.tag + " | " + hit.collider.transform);
if (Players.Script.PlayersTurn == TurnStates.Player_1_Turn)
{
// Instantiate Red
Instantiate(Counter_Manager.Script.RedCounter, hit.transform, hit.transform);
}
else
{
// Instantiate Blue
}
}
}
else
{
print("Our collider is null");
}
}
I hope someone can help.
Thanks.