• 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 Rxanadu · Oct 01, 2013 at 09:10 PM · raycastdistancelinerendererraycasthitprojectile

Drawing a line in front of player weapon

I trying to draw a line from a the nozzle of a gun to a position which is a set distance away from the gun. This line is supposed to indicate the distance projectiles can travel to. I should act similarly to a laser pointer but with a very distinct indicator to show how far the projectile can travel before being destroyed/ turned inactive and placed into an object cache/etc.

I've created a simple script to test this line out using Raycast, which uses a RaycastHit variable to set the position of the indicator. However, the indicator sticks to surfaces the raycast has hit; it does not move in front of the gun as I intended.

Here is the specific block of code I'm having issue with:

 void ShowDestinationLine() {
         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0)); ;
         RaycastHit hit = new RaycastHit();
         bool foundHit = false;
         //travelDestination = transform.position + Vector3(transform.position.x, trasnform.position.y, transform.position.z + travelDist);
 
         if (Physics.Raycast(ray, out hit, travelDist))
         {
             foundHit = true;
         }
         else
             foundHit = false;
 
         if (foundHit)
         {
             travelLine.SetPosition(0, transform.position);
             travelLine.SetPosition(1, hit.point);
             destinationMarker.transform.position = hit.point;
             //cube.rotation = Quaternion.LookRotation(hitInUse.normal);   //normal sets object rotation to be away from hit rotation
         }
     }    

In this case, I'm not sure where to place the indicator (called "destinationMarker" in code) in relation to raycast.

If anyone has any idea where to place the indicator, I would appreciate any push in the right direction.

I intend to improve the functionality of the line, so if the raycast hits a surface that is a shorter distance than the specified distance for the ray to extend, the indicator shows up at that hit point, instead. I'm not sure if the initial solution will automatically solve this problem, so I have placed this reminder for myself here.

Comment
Add comment · Show 1
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 mattssonon · Oct 02, 2013 at 09:39 AM 0
Share

How are you calling this function? You should re-call it whenever the player moves.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How can I can I cast a ray from a gameobject? 1 Answer

RaycastHit.distance 1 Answer

Raycasts don't go the right direction unless i'm really far away (javascript) 1 Answer

Raycasting, How to draw a line to the position of the rays end when nothing is hit 1 Answer

LineRenderer - Setting Max Distance 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