Hello, I’m making a simple RTS game and I need to implement a selecting feature. In fact mu script works very well but the problem is that I also instantiate prefabs. And because these prefabs are buildings on a moving planet, I set the parent of those instantiated prefabs to the Planet they’re build on, so they remain on it when the Planet moves.
However, when I do this, and try to select the building, it is the Planet that is selected, and after some testing it would seem that the reason of that is because the building is the child of the planet.
So here’s my question; is it possible to make this selecting script select the building and not the Planet is is built on, and if not, is there any way to make the position of the building relative to the Planet unchanged when the Planet moves without making the building a child of the Planet.
The selection is made with OnMouseUp().
–EDIT–
As suggested tried to do with Raycast, here’s the code:
using UnityEngine;
using System.Collections;
public class SelectingMicros : MonoBehaviour {
void Update()
{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hitInfo = new RaycastHit();
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo) && hitInfo.transform.tag == "Construction")
{
print ("It's working");
}
}
}
}
This doesn’t work when the building is the child of the planet. I click on the building but the message doesn’t appear, while if I put the tag of the Planet to “Construction” as well, it works but selects the Planet, not the building.
Given your question “is it possible”, almost everything is possible. Probably your are doing a basic error.Generally input selection there is no influence about your objects positions and hierarchy. Create some prototypes and take a look again in documentation:
EDIT: Added more info
raddry, try to put more debugs to know where exactly it is failing. Like:
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse is down");
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "Construction")
{
Debug.Log ("It's working!");
} else {
Debug.Log ("nopz");
}
} else {
Debug.Log("No hit");
}
Debug.Log("Mouse is down");
}
}
Remember that raycast hit only objects with Colliders, if I have a child with collider, the child.transform will be referenced in RaycastHit.
Example: Given GameObject HQ that have 2 children, Model with a renderer and Bound with Collider, I will got the Bound.
HQ (tag=construction)
|- Model (Renderer)
\- Bound (BoxCollider)
If I raycast this, I will got the Bound object. You can verify parent tag:
var hitConstruction = hitInfo.transform.parent.gameObject.tag == "Construction"
// this code show nameobject with click
if (Input.GetMouseButtonDown(0))
{
//empty RaycastHit object which raycast puts the hit details into
var hit : RaycastHit;
//ray shooting out of the camera from where the mouse is
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, hit))
{
//print out the name if the raycast hits something
Debug.Log(hit.collider.name);
//freeclup= hit.collider.name;
}
}