Is there a script that allows you to spawn a game object a few meters in front of your FPS controller.
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!
Answer by jdean300 · Jun 15, 2016 at 07:13 AM
//"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);
maybe add a coefficient to the position ( pos = transform.position + transform.forward * distance
) to show where to distance management is.
Answer by chuggy25 · Jun 15, 2016 at 05:46 PM
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]
Sorry for the wonky code using UnityEngine; using System.Collections; using System;
public class Box : $$anonymous$$onoBehaviour {
public GameObject GameObject;
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.Get$$anonymous$$ouseButtonDown(0))
GameObject.transform.position = player.transform.position + transform.forward * 5;
//Facing this object
var rot = transform.rotation * Quaternion.Euler(0, 180f, 0);
var obj = (GameObject);
//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.$$anonymous$$.y - 12;
obj.transform.position -= new Vector3(0, distFromGround, 0);
The only thing I can see that might be an issue is you probably want var rot = player.transform.rotation * Quaternion.Euler(0, 180f, 0);
I can't really tell what the issue is from the picture, you need to give a better explanation.
The error is that the game object (black, lined cube ) is appearing to the right, and below my player. I need the cube to appear in front of the player, and always be facing the him/her. Someone suggested a ray-cast script might be an easier way to do it. But this Is what I've got at the moment.
Your answer
Follow this Question
Related Questions
How would you create a 2D endless runner with objects moving in the background? 0 Answers
Prefab to automatically follow a waypoint path? 1 Answer
How can I have prefab spawned at the game's start and at a position? 0 Answers
GameObject doesn't move 1 Answer
How can I instantiate a object on the world coordinates 1 Answer