- Home /
Trying to get a game object to spawn right above another game object instead of the point where the collider hit.
I'm using a box collider attached to my player's head to check if there are any items on the ground when he's looking down and instantiate a tooltip with the item's name, stats etc.
So I have everything pretty much working except for the fact that when he looks at a game object on the ground the tooltip instantiates at the exact point where the collider hit, and for larger items this is an issue because the tooltip instantiates halfway inside the item and is unable to be read.
So what I'm trying to get is whenever that box collider hits it instantiates the position of the tooltip right above the item and (optional) slowly follows my players head if he starts looking left or right until the collider isn't hitting it the item anymore
Here's what I have so far:
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Item") {
GameObject item = other.gameObject;
Vector3 itemvec = item.GetComponent<Renderer>().bounds.center;
//Vector3 itemvec = new Vector3 (item.transform.position.x, item.transform.position.y + 0.5f, item.transform.position.z);
if (infoBox != null) {
return;
}
else {
infoBox = (GameObject) Instantiate (uiWindow, itemvec, cam.transform.rotation);
//infoBox.transform.LookAt (cam.transform.position);
//infoBox.transform.LookAt (other.gameObject.transform);
}
}
void OnTriggerExit (Collider other) {
Destroy (infoBox);
}
(You could tell above I tried uses render.bounds.center but it wasn't working)
By the way I'm not using raycast because I'm trying to get a sort of cone effect from the players vision and instantiate tooltips for any items the box collider sees in front of him :) (and I'm still a noob and raycasts kind of confuse me)
Answer by toddisarockstar · May 15, 2017 at 04:21 AM
I know you don't like it but raycast it is an obvios soulution to your problem. Alternatively you could make empty gameobjects parented inside your models to represent positions above the models for reference. then look for them on collisions. empty position markers are handy sometimes.
anyways here is the raycast:
if (other.gameObject.tag == "Item") {
Vector3 itemvec;
GameObject item = other.gameObject;
RaycastHit rch;
Vector3 v;
v=new Vector3(item.transform.position.x,100,item.transform.position.z);
if(Physics.Raycast(v,-Vector3.up,out rch)){
itemvec=rch.point;
print(itemvec+" is top of your object's collider");
}
}