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
-1
Question by Xsnip3rX · Mar 06, 2013 at 02:10 AM · targetweaponlaserbeam

Beam Weapon

Sorry if this has already been asked before a million times, but i don't quite understand how i would combine these 2 scripts, i need my ship to fire a directed energy beam at the target reticle...

Here is the beam script

 @script RequireComponent (LineRenderer)
 
     var mouse : Vector2;
     var hit : RaycastHit;
     var range : float = 100.0;
     var line : LineRenderer;
     var lineMaterial : Material;
     var ray : Ray;
  
     function Start()
     {
        line = GetComponent(LineRenderer);
        line.SetVertexCount(2);
        line.renderer.material = lineMaterial;
        line.SetWidth(0.1f, 0.25f);
     }
  
     function Update() {
         if (ship) {
         //var myShipClass = GetComponent("shipClass");
         target = ship.GetComponent(shipClass).Get_Target();
     }
        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if(Physics.Raycast(ray, hit, range))
        {
          if(Input.GetMouseButton(0))
          {
           line.enabled = true;
           line.SetPosition(0, transform.position);
           line.SetPosition(1, hit.point + hit.normal);
          }
          else
           line.enabled = false;
        }
  
     }

And the Reticle Script:

 private var target:GameObject = null;
 var reticle:Texture2D;
 var ship:GameObject;
 private var guiCrossPos:Vector3 = Vector3(0,0,0);
 private var guiCrossSize:Vector2;
 function Start () {
 
 }
 
 function Update () {
     if (ship) {
         //var myShipClass = GetComponent("shipClass");
         target = ship.GetComponent(shipClass).Get_Target();
     }
     if (target && reticle) {
         guiCrossSize = Vector2(135,100);
         guiCrossPos = camera.WorldToScreenPoint(target.transform.position);
         guiCrossPos.x -= guiCrossSize.x / 2;
         guiCrossPos.y += guiCrossSize.y / 2;
         guiCrossPos.y = Screen.height - guiCrossPos.y;
         print(guiCrossSize);
     }
 }
 
 function OnGUI () {
     if (target && reticle) {
         GUI.DrawTexture(Rect(guiCrossPos.x,guiCrossPos.y,guiCrossSize.x,guiCrossSize.y),reticle);
     }
 }
Comment
Add comment · Show 5
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 Chronos-L · Mar 06, 2013 at 04:12 AM 0
Share

Format the question please, it hurts my eye trying to read the 2nd script. By the way, [code]...[/code] will not work here.

avatar image Xsnip3rX · Mar 06, 2013 at 04:19 AM 0
Share

Thanks, basically what i'm trying to do is this:

http://youtu.be/rm$$anonymous$$aFLHrG_U?t=1m26s

avatar image Xsnip3rX · Mar 06, 2013 at 10:23 PM 0
Share

Can anyone please assist with this?

So i ask someone to help me with this and i get a down vote? that's not very nice.

avatar image hatshapedhat · Mar 06, 2013 at 11:23 PM 0
Share

I think you got downvoted because it isn't clear what you have tried, what problems you are encountering, or what question you are asking. Try to boil it down a little further.

avatar image Xsnip3rX · Mar 06, 2013 at 11:25 PM 0
Share

Well, my beam goes flying around to objects other than what the Targeting Reticle has selected.

1 Reply

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

Answer by robertbu · Mar 07, 2013 at 12:21 AM

You likely got a down vote because you posted a question with very little explanation about what code was doing now or how you wanted it to behave. Nor did you show any understanding of the code in your comments, which pushed the question towards "do it for me" rather than "help me."

The Reticule code simply displays the retcule at target position projected onto the screen. The first script finds targets based on projecting a ray from the mouse position into the scene and looking at what it hit. You don't need to actually combine the two scripts (though you can), but you need to change the first script so that it targets in the same way. In looking at the script, here are some completely untested changes that will point you in the right direction:

 function Update() {
     if (ship) {
    //var myShipClass = GetComponent("shipClass");
    var target = ship.GetComponent(shipClass).Get_Target();
     }
    //ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    //if(Physics.Raycast(ray, hit, range))
    //{
      if(Input.GetMouseButton(0))
      {
       line.enabled = true;
       line.SetPosition(0, transform.position);
       //line.SetPosition(1, hit.point + hit.normal);
       line.SetPosition(1, target.transform.position);
      }
      else
       line.enabled = false;
    //}
    }

Note given the parameters above, the LineRenderer component must have the the "Use World Space" turned on.

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 Xsnip3rX · Mar 07, 2013 at 07:26 AM 0
Share

Thank you good sir, that got me a good bit on my way to figuring this beast out :)

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

12 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

Related Questions

Character lasergun? 1 Answer

Creating laser gun effect. 1 Answer

How to draw beams? 2 Answers

Look at target and flip 1 Answer

Ray Gun / Laser Beam 3 Answers


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