I can’t seem to make a script that spawns my game object at the right height, rotation, and direction. I need it to spawn a meter in front of my character, and level (or close to level) with the ground. I can get as far as spawning the object directly to the right of my player, but it is half underground and doesn’t take into account the direction my player is facing.
Any help is appreciated!
//“distance” meters in front of this object
var distance = 1f;
var pos = transform.position + transform.forward * distance;
//Facing this object
var rot = transform.rotation * Quaternion.Euler(0, 180f, 0);
var obj = (GameObject) Instantiate(ObjectPrefab, pos, rot);
//Find the renderer on this object and get its bounds (now we know how tall it is)
//alternatively, if you have a collider that is larger than the rendered bounds,
//you could get the colliders bounds
var bounds = obj.GetComponent<Renderer>().bounds;
//Now move the object up/down to be level with the ground
var distFromGround = bounds.min.y - groundYLevel;
obj.transform.position -= new Vector3(0, distFromGround, 0);
Thanks for your help guys,
As I am new to this I don’t really know how to tailor this to my own use, so I was forced to add the few c# variables I know how to work with. Below is the edited script, and a picture of the game object spawn location when using the attached script.![alt text][1]