- Home /
How to create spawnPoint for bullet shoot
hello and Hi I am developing an game which includes a gun, now i want to shoot bullet with gun i have made bullet prefab and applied fire script on gun but i don't know how to create spawnPoint. my gun is firing bullet if i m naming one of the game object as spawnPoint . but i want atleast a dot as i am aiming to object then i will fire to that dot point ...(spawnPoint)
using this simple fire code ...
var BulletPrefab :Transform;
var force : float = 2;
function Update()
{
if(Input.GetButtonDown("Fire1"))
{ var bullet = Instantiate(BulletPrefab,GameObject.Find("spawnPoint").transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * force);
} }
Answer by DMCH · Feb 15, 2013 at 03:19 PM
Hello ParmeshWar,
I think you are asking 2 questions here.
1) How to create a spawnpoint for your bullets
2) How to display an aiming reticule
1) To create a spawnpoint for bullets, create an empty gameobject, and position it just beyond the barrel of your gun. Make sure it is rotated in whichever direction you want your bullets to go. Now make it a child gameobject of your character/gun. (Do this by dragging the empty gameobject onto your character or gun)
Declare a field in whatever script you are instantiating the bullets in, and assign the empty gameobject to it in the inspector. Assuming you called the spawnPoint "bulletspawn" in your script, you could instantiate bullets from the spawnpoint like this:
Instantiate(bullet, bulletSpawn.transform.position, bulletSpawn.transform.rotation);
Also, have a look at this tutorial, it's very close to what you need: http://www.youtube.com/watch?v=enLEpL0DrPM&list=PL11F87EB39F84E292∈dex=4
Hope this helps!
thanks @DHC$$anonymous$$ now i am really interested in answer of the second question that you have noticed... i managed up till now besides aim point.... will u please give me any reference for ai$$anonymous$$g reticule
Sorry, didn't see the reply. I'm not too sure about the reticule part. The only solution that comes to $$anonymous$$d is using OnGUI to draw a label at the centre of the screen. This should give you a start:
private Texture2D reticule;
void OnGUI()
{
Rect pos = new Rect(Screen.width/2 - reticule.width/2 , Screen.height/2 - reticule.height/2,reticule.width, reticule.height);
GUI.Label(pos, reticule);
}
Hope this is of use. Consider accepting the answer if it has helped : )