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 stuart6854 · Oct 14, 2013 at 03:39 PM · shootingraycastinglinerendererlaser

Laser Beam using Raycasting & Line Renderer

Hello,

I am trying to us Line-Renderer to be able to see my Space-Ship shoot lasers, but when i do render the line it goes from the laser origin cube to the ship instead of the the actual target?

Raycast Code:

 var laserOriginOne : GameObject;
 var hitOne : RaycastHit;
 
 function Update () {
 if(Input.GetButton("Fire Laser")){
             if(Physics.Raycast(laserOriginOne.transform.position, Vector3.forward)){
             
             laserOriginOne.GetComponent(ShipFire).FireLasers(hitOne);
             
             Debug.Log(hitOne.transform);
             }
         }
 }

'FireLasers()' Code:

 var lineOne : LineRenderer;
 
 function Start() {
     lineOne = GetComponent(LineRenderer);
     
     lineOne.enabled = false;
     
     lineOne.SetVertexCount(2);
     
     lineOne.SetWidth(0.1f, 0.25f);
 }
 
 function FireLasers(hitOne : RaycastHit){
     lineOne.enabled = true;
     lineOne.SetPosition(0, transform.position);
     lineOne.SetPosition(1, hitOne.point);
 }

Like i said i don't know why the laser goes from the 'laserOriginOne' to my 'ship'?

I have tried a few thing such as moving my ship away from the laserOrigin blocks.

Thanks for any help!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Oct 14, 2013 at 04:03 PM

You're never setting the hitOne variable, so it stays all 0's. That debug line should be showing you that. Change the raycast command so it does, by adding hitOne at the end:

 Physics.Raycast(laserOriginOne.transform.position, Vector3.forward, hitOne))

Take a look at the script reference for Raycasts. It's a little tricky -- hitOne is an output parameter, which means it starts empty (like you have it) and is filled in by the ray cast. If you don't send it, the ray cast doesn't write the hit position anywhere.

There's also probably a problem with using Vector3.forward for the direction. That will always shoot the laser due North. Just in general, you might want to browse through a bunch of other raycast examples here (shooting is a popular topic.) Raycasts use some tricky programming stuff.

Comment
Add comment · Show 2 · 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 stuart6854 · Oct 14, 2013 at 04:10 PM 0
Share

Thanks that worked, as for the Vector3.forward, i changed that to be a variable called 'fwd' which is set with this:

 var fwd = laserOriginOne.transform.TransformDirection(Vector3.forward);
avatar image Owen-Reynolds · Oct 14, 2013 at 05:23 PM 0
Share

A shortcut for "my forward" is transform.forward (no TransformDirection needed.) It's pretty common in snippets of Unity code. TransformDirection can be saved for heavy-duty rotational math.

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

15 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

Related Questions

Draw line from Player to Mouse position 0 Answers

raycast is pointing in two directions? 1 Answer

Script for shooting with a LineRender (Laser Weapon) 1 Answer

Raycast does not detect colliders properly when move object(LineRenderer) with high speed,Raycast does not detect Collider when draw line with fast speed using Input.mousePosition() 0 Answers

Raycasting along line renderer width 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