• 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
Question by samaxi · Jan 28, 2014 at 11:19 PM · guiraycast3dscreenshooting

Raycast shooting in the middle of the screen

I need help with my raycast shooting, it isn't shooting in the middle of my cross but my script should have it shooting in the middle of my screen. Please help

Shooting script

 #pragma strict
 
 var damage:float = 100;
 var range = 8000.0;
 var force = 10.0;
 var muzzleFlash : Renderer;
 var muzzleLight : Light;
 
 var SniperMenu: GUIStyle;
 var ReloadMenu: GUIStyle;
 
 var ReloadWarning = false;
 
 var cameraTransform: Transform;
 
 static var _SniperCanReload = false;
 
 var clipSize: int = 5;
 var bulletsInClip = 5;
 var bulletsLeft = 15;
 static var allowfire : boolean = true;
 
 function Start (){
 muzzleFlash.enabled = false;
 muzzleLight.enabled = false;
 }
 
 function Update () {
 if(Input.GetMouseButtonUp(0)){
 if(bulletsInClip >= 1){
 if(allowfire == true){
 fire();
 }
 }
 }
 if (Input.GetKeyDown (KeyCode.R)){
 if(bulletsInClip == 0){
 if(bulletsLeft >= 5){
 Reload();
 }
 }
 }
 if(bulletsInClip == 0){
 if(bulletsLeft >= 5){
 ReloadWarning = true;
 }
 } else {
 ReloadWarning = false;
 }
 if(bulletsInClip == 0){
 if(bulletsLeft >= 5){
 _SniperCanReload = true;
 }
 } else {
 _SniperCanReload = false;
 }
 }
 
 function OnGUI() {
 if(ReloadWarning == true){
 GUI.Label (Rect (Screen.width / 2.2, Screen.height / 2.5, 200, 200), "Press R to reload.", ReloadMenu);
 }
 GUI.Label (Rect (10, 650, 350, 350), "Sniper", SniperMenu);
 GUI.Label (Rect (10, 700, 350, 350), bulletsInClip + " / " + bulletsLeft, SniperMenu);
 }
 
 function fire(){
 allowfire = false;
 audio.Play();
 bulletsInClip--;
         
 var direction = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;
 
 // Did we hit anything?
 if (Physics.Raycast (cameraTransform.position, cameraTransform.forward, hit)) {
 Debug.DrawRay(transform.position, direction * range);
 // Apply a force to the rigidbody we hit
 if (hit.rigidbody)
 hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
 hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }
 
 muzzleFlash.renderer.enabled = true;
 muzzleLight.enabled = true;
 yield WaitForSeconds (0.04);
 muzzleFlash.renderer.enabled = false;
 muzzleLight.enabled = false;
 
 
 yield WaitForSeconds(0.1);
 allowfire = true;
 }
 
 function OnTriggerEnter(hit:Collider){
 if(hit.tag == "Enemy"){
 hit.transform.SendMessage("Damage",damage);
 }
 }
 
 function Reload(){
 allowfire = false;
 yield WaitForSeconds (1);
 bulletsInClip = clipSize;
 bulletsLeft = (bulletsLeft - clipSize);
 yield WaitForSeconds (0.3);
 allowfire = true;
 }

Crosshair Script

 var crosshairTexture : Texture2D;
 var position : Rect;
  
 function Start()
 {
     position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - 
     crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
 }
 
 function OnGUI()
 {
     GUI.DrawTexture(position, crosshairTexture);
 }
Comment
Jessy

People who like this

-1 Show 4
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 MrCrinkle · Jan 28, 2014 at 11:37 PM 0
Share

Are you setting cameraTransform anywhere? I see it being reference but not initialized.

avatar image RyanZimmerman87 · Jan 28, 2014 at 11:38 PM 0
Share

Hmm not too much jumping out at me from your code but I use C#.

So where is it shooting if not the middle of the screen?

I'm assuming this script is attached to the camera and the cameraTransform variable is assigned correctly?

A little confused since you are using transform for the DrawRay but cameraTransform for the Raycast? Not sure if they are both the same transform since I'm so familiar with C#. I'm guessing using just "var" is a public variable and you did assign the cameraTransform in the Unity Editor?

Hmm, if you assigned everything correctly the only thing I can think of is to check out the camera position if it's a child object. You could possibly have to use local rotation or transform.parent stuff but it's really just impossible to tell for me with just the code.

Probably not much help but that's all I got unfortunately.

avatar image getyour411 · Jan 29, 2014 at 12:31 AM 0
Share

In your crosshair script, I think your position is off. You are subtracting the entire width of the texture, I believe you want to div that by /2 first, then take the result and div by 2 as you are. Since you are taking the entire width, your left offset will be off a bit, same for height.

avatar image samaxi · Jan 29, 2014 at 03:22 PM 0
Share

It doesn't hit the middle of my cross hair, which I changed the script for.

 var crosshairTexture : Texture2D;
 var position : Rect;
  
 function Start()
 {
     position = Rect(Screen.width / 2, Screen.height / 2, crosshairTexture.width, crosshairTexture.height);
 }
 
 function OnGUI()
 {
     GUI.DrawTexture(position, crosshairTexture);
 }

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image

Related Questions

GUI Follow RaycastHit 2 Answers

FPS shooting 1 Answer

Displaying score on screen in specific place 1 Answer

Keeping a GUI texture from moving during change of screen size 2 Answers

GUI.DrawTexture Center on screen 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