- Home /
Instantiating GO infront of player
I'm trying to instantiate a gameobject right infront of the player. I can get the obj to instantiate infront but only in one direction (forward). If the player turns, when the item is spawned again it's in the same position initially but not where the new position of the player who has turned.
bulletSpawnPoint is a GO that I placed infront of the player and set the GO as a child of Player.
Basically I'm looking for when he turns the ability to shoot the bullet infront of the player.
If player hits space
Bullet = (GameObject)Instantiate(Resources.Load("Bullet"), GameObject.Find("bulletSpawnPoint").transform.position, Quaternion.identity);
Inside of Update() boolean checks to see if bullet = true if he shot a bullet. And it's setup with a timer to 2 secs for him to shoot a bullet
star.transform.Translate(Vector3.forward * Time.deltaTime * 20, Space.Self);
substitute transform.rotation ins$$anonymous$$d of Quaternion.Identity (as the last paramater to the Instantiate() function); check my post.
Answer by mrde · Feb 08, 2011 at 12:24 AM
your bulletSpawnPoint GO must be child of Player/Main Camera
Answer by lampshade · Feb 08, 2011 at 03:37 AM
The way that I typically instantiate in front of the player is by attaching a script to the player and adding the following code:
public class Projectiles : MonoBehaviour { public GameObject prefab; public float speed = 1025f;
private void Update() {
if (Input.GetKeyDown("Fire1")) FireProjectiles();
}
private void FireProjectiles() {
GameObject instPrefab = (GameObject) Instantiate(prefab, transform.position, transform.rotation);
instPrefab.rigidbody.velocity = transform.TransformDirection(Vector3.forward * speed);
}
}
Your answer
Follow this Question
Related Questions
Script to Keep One GameObject from Being Destroyed 0 Answers
in-editor cloning of game objects 1 Answer
Instantiate fails with no errors 0 Answers
Instantiate rigidbody, transform or gameobject? 1 Answer
Convert FileStream to Gamobject? 0 Answers