- Home /
Make bullet launch at center of screen
I've tried searching around for a code to fire a bullet to center of screen I know it has to do with ray cast but I don't know how to implement it in my code Thanks
var bullet : Rigidbody;
var power : float = 1500;
var damage : float = 100;
var reloadtime : float = 1.5;
var magcount : int = 10;
var magbulletcount : int = 9;
var bulletcount : int = 0;
var rtext : boolean = false;
var bulletTex : GameObject[];
var firing : boolean = false;
var clip : AudioClip;
private var reloadTimer: float = 0.0; // internal reload timer
function Start () {
bulletcount = magbulletcount;
}
Screen.showCursor = false;
function Update(){
if (reloadTimer > 0){ // if reloadTimer active...
rtext = true;
reloadTimer -= Time.deltaTime; // decrement it
if (reloadTimer <= 0){ // if reloadTime ended...
rtext = false;
bulletcount = magbulletcount; // load a full clip...
magcount--; // and decrement clip count
}
}
else // only shoot when reloadTimer not active
if (Input.GetButtonDown("Fire1")&& bulletcount > 0){
var instance : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd : Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
var hit : RaycastHit;
firing = true;
bulletcount --;
if(Input.GetButtonDown ("Fire1") && Physics.Raycast(transform.position, fwd, hit, 10)&& bulletcount > 0){
Instantiate(bulletTex[Random.Range(0,3)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
}
if (bulletcount <= 0 && magcount > 0){
// if run out of ammo but still have clips...
reloadTimer = reloadtime; // activate reloadTimer
// you can play reload sound or animation here
}
}
if (Input.GetKeyUp(KeyCode.R) && magcount > 0 && bulletcount < magbulletcount){
reloadTimer = reloadtime;
}
if(firing && !audio.isPlaying){
audio.PlayOneShot(clip);
}
if(!Input.GetButtonDown("Fire1")){
firing = false;
}
}
var originalWidth = 1024.0; // define here the original resolution
var originalHeight = 768.0; // you used to create the GUI contents
private var scale: Vector3;
function OnGUI(){
scale.x = Screen.width/originalWidth; // calculate hor scale
scale.y = Screen.height/originalHeight; // calculate vert scale
scale.z = 1;
var svMat = GUI.matrix; // save current matrix
// substitute matrix - only scale is altered from standard
GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, scale);
// draw your GUI controls here:
GUI.contentColor = Color.yellow;
GUI.Box(Rect(10,10,200,55), "Lawrence Mercieca's game test \n \n(PRE ALPHA TESTING PHASE)");
GUI.Button(Rect(20, 680, 50, 30),bulletcount+" / "+magcount);
if (rtext){
GUI.Button(Rect(20, 720, 100, 30), "RELOADING");
//...
// restore matrix before returning
GUI.matrix = svMat; // restore matrix
}
}
Answer by robertbu · Oct 21, 2013 at 11:36 PM
Firing from the center of the screen is a special case. You don't need a raycast or any other kind of conversion unless you need it for targeting. You can fire from any point directly in front of the middle of the screen in the direction of the camera forward. There are multiple ways to make this happen, but this is what I suggest:
In the editor place the camera at (0,0,-10) with rotation (0,0,0).
Create an empty game object however far in front of the middle of the camera you want to spawn. It will have a position like (0,0,-9.5) with rotaiton (0,0,0).
If the camera moves, make the empty game object (spawn point) a child of the camera.
Reset the camera to whatever you are using for your game.
As for firing the bullet, your current code will do the job:
var instance : Rigidbody = Instantiate(bullet, transform.position, transform.rotation);
var fwd : Vector3 = transform.TransformDirection(Vector3.forward);
instance.AddForce(fwd * power);
Note that if you have gravity, your bullet will drop some under its influence before hitting the target.
7 years late, but THANK YOU for this. It was driving me insane.
Answer by Cherno · Oct 21, 2013 at 11:10 PM
Also, check the User Reference for Physics.Raycast for a description of parameters and how to use them.