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 goo-muffin · Jan 27, 2013 at 11:44 AM · rotationfpsguncrosshairlaser

shooting laser to screen middle doesnt work...

Hi! This is a quite complex question. I got a lasergun shooting a laser forward to where I look until it hits something. When it doesnt hit something it works perfect. He shoots so that the laserpointer ends at the screenmiddle. But if it comes closer to an object it doesnt shoot to the 3d point of the Screen middle... Here a picture:

alt text

Does it has something to do with the angle of my gun? Or how can I fix this?

BTW: My script looks like this:

 var lineRenderer :LineRenderer;
 @HideInInspector
 var startPos :Vector2;
 var maxLength :float = 1000;
 
 var multiplier :float = 5;
 
 var duration :int;
 var isshooting :boolean;
 var currentduration :int;
 var point :Texture;
 var line :Texture;
 
 var chWidth :float;
 var length :float;
 var c :Camera;
 
 function Start(){
     lineRenderer.useWorldSpace = false;
 }
 
 function Update(){
 
     
     if(Input.GetMouseButtonDown(0)){
         if(isshooting == false){
             isshooting = true;
             currentduration = 0;
         }
     }
     if(Input.GetMouseButton(0)){
         if(isshooting == true){
             if(currentduration <=duration){
                 isshooting = true;
                 currentduration ++;
             }else{
                 isshooting = false;
             }
         }
     }else{
         isshooting = false;
     }    
     
     if(isshooting){
         var hit :RaycastHit;
         Physics.Raycast(transform.position,transform.forward,hit);
         if(hit.collider != null){
         
             lineRenderer.SetPosition(1, Vector3(startPos.x, startPos.y, hit.distance * multiplier));
 
         }else{
             lineRenderer.SetPosition(1, Vector3(startPos.x, startPos.y, maxLength));
 
         }
     }else{
         lineRenderer.SetPosition(0, Vector3(0,0,0));
         lineRenderer.SetPosition(1, Vector3(0,0,0));
     }
     
 }
 
 function OnGUI(){
     /*GUI.DrawTexture(Rect(Screen.width / 2 - chWidth / 2 - 1, Screen.height/2 -length/2, 2, length), line);
     GUI.DrawTexture(Rect(Screen.width / 2 + chWidth / 2 - 1, Screen.height/2 -length/2, 2, length), line);
     GUI.DrawTexture(Rect(Screen.width / 2 - length / 2, Screen.height/2 - chWidth/2 - 1, length, 2), line);
     GUI.DrawTexture(Rect(Screen.width / 2 - length / 2, Screen.height/2 + chWidth/2 - 1, length, 2), line);*/
     GUI.DrawTexture(Rect(Screen.width / 2 - 3, Screen.height / 2 - 3, 6, 6), point); 
 }

false.png (170.5 kB)
Comment
Add comment · 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 FL · Jan 27, 2013 at 11:52 AM 0
Share

This isn't a laser error. You need to update the laserpointer to reflect when you have some object in front, like a real laserpointer.

If you laser won't come exact from middle, you can't shoot at exact middle without a certain distance.

avatar image goo-muffin · Jan 27, 2013 at 11:54 AM 0
Share

so is this an unfixable error? There must be a way to create a crosshair!

avatar image goo-muffin · Jan 27, 2013 at 11:55 AM 0
Share

so the laser should be shot from the middle to my gun?

avatar image FL · Jan 27, 2013 at 12:22 PM 0
Share

I believe that you need to shot from the middle of screen or update the laserpointer when you have a object in your front if you wish to make it accurate.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by goo-muffin · Jan 27, 2013 at 01:06 PM

 var lineRenderer :LineRenderer;
 @HideInInspector
 var startPos :Vector2;
 var maxLength :float = 1000;
 
 var multiplier :float = 5;
 
 var duration :int;
 var isshooting :boolean;
 var currentduration :int;
 var point :Texture;
 var line :Texture;
 
 var chWidth :float;
 var length :float;
 var c :Camera;
 var startPos2 :Vector3; 
 var offset :Vector2;
 function Start(){
     lineRenderer.useWorldSpace = true;
 }
 
 function Update(){
 
     startPos2 = transform.position;
     if(Input.GetMouseButtonDown(0)){
         if(isshooting == false){
             isshooting = true;
             currentduration = 0;
         }
     }
     if(Input.GetMouseButton(0)){
         if(isshooting == true){
             if(currentduration <=duration){
                 isshooting = true;
                 currentduration ++;
             }else{
                 isshooting = false;
             }
         }
     }else{
         isshooting = false;
     }    
     
     if(isshooting){
         var hit : RaycastHit;
         var ray : Ray = c.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
         lineRenderer.SetPosition(0, startPos2);
         if (Physics.Raycast (ray, hit, 100)){
             lineRenderer.SetPosition(1, hit.point);
             print("hit");
         }else{
             lineRenderer.SetPosition(1, c.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, maxLength)));
         }
     }else{
         lineRenderer.SetPosition(0, Vector3(0,0,0));
         lineRenderer.SetPosition(1, Vector3(0,0,0));
     }
     if(Input.GetKeyDown("f")){
         if(Time.timeScale == 0){
             Time.timeScale = 1;
         }else{
             Time.timeScale = 0;
         }
     }
 }
 
 function OnGUI(){
     GUI.DrawTexture(Rect(Screen.width / 2 - chWidth / 2 - 1, Screen.height/2 -length/2, 2, length), line);
     GUI.DrawTexture(Rect(Screen.width / 2 + chWidth / 2 - 1, Screen.height/2 -length/2, 2, length), line);
     GUI.DrawTexture(Rect(Screen.width / 2 - length / 2, Screen.height/2 - chWidth/2 - 1, length, 2), line);
     GUI.DrawTexture(Rect(Screen.width / 2 - length / 2, Screen.height/2 + chWidth/2 - 1, length, 2), line);
     GUI.DrawTexture(Rect(Screen.width / 2 - 3, Screen.height / 2 - 3, 6, 6), point); 
 }

I got it on my own

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 sparkzbarca · Jan 28, 2013 at 05:27 AM 0
Share

yea you'd want to shoot out to the middle then whatever you hit have the gun look at that so its aimed at it.

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

10 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

Related Questions

FPS Run Animation Gun - Rotation Issue 0 Answers

laser pointer 1 Answer

I need help with a firing script 1 Answer

Gun not rotating with the camera? 1 Answer

Rotation/position question. 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