Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lawranca · Oct 21, 2013 at 10:36 PM · raycastguncentergunfire

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
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image koookee · Sep 12, 2020 at 05:19 AM 0
Share

7 years late, but THANK YOU for this. It was driving me insane.

avatar image
0

Answer by Cherno · Oct 21, 2013 at 11:10 PM

Everything you need

Also, check the User Reference for Physics.Raycast for a description of parameters and how to use them.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Using RayCast to do Continuous Shooting? 1 Answer

How do I create a raycast from the end of my gun to go wherever i have my gun pointed 2 Answers

Gun is not shooting? Please Help! 3 Answers

Reload clips on Gun-script not applied properly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges