• 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 Arnout Swint · May 26, 2011 at 09:37 AM · physicsraycastraycastingdistancehit

Raycast stop at distance

Hey!

I got a question..

I'm using raycasting for shooting with the mouse only problem is, that my raycast needs to stop at the mousepointer. The variable for that is "hit.point (Vector3)" or just "hit (RaycastHit)".

Let me explain:

alt text

for some reason:

 var direction = transform.TransformDirection (Vector3.forward) * 50000;
 var hit : RaycastHit;
 var ray2 : Ray = new Ray(transform.position,hit.point - transform.position);
 var Layermask = 1<<8;
 
 if (Physics.Raycast (ray2.origin, direction, hit, Layermask) ) {.....
 

so Physics.Raycast (Vector3,Vector3,float,layerMask) doesn't work, eventhough the reference sais:

 static function Raycast (origin : Vector3, direction : Vector3, distance : float = Mathf.Infinity, layerMask : int = kDefaultRaycastLayers) : bool



Please help me :)

Comment
Add comment · Show 3
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 Bunny83 · May 26, 2011 at 10:38 AM 0
Share

I'm not sure but do you want to cast a ray against another ray? That's impossible with Raycast functions. Raycasts only works against collider. Plane.Raycast is also a way but i don't understand what you want to do...

It's even almost impossible that you hit another line in 3D space. In most cases they will miss each other and there's just a point where they come quite close.

If you can explain what you want to do we find a solution.

avatar image Bunny83 · May 26, 2011 at 10:42 AM 0
Share

Actually, where's your first ray? Another thing: You create a variable hit that is uninitialized and in the next line you use hit.point which will be (0,0,0) all the time. Actually you don't even use the ray2 you just use the orgin which equals transform.position

avatar image Arnout Swint · May 27, 2011 at 06:49 AM 0
Share

$$anonymous$$y first ray = Camera.main.ScreenPointToRay(Input.mousePosition). I want the ray2 length to be from transform.position (or ray2.origin) to hit.point.

3 Replies

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

Answer by themusicdork · May 26, 2011 at 09:50 AM

try using

Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),hit,DISTANCE);

of course you would use however far you would like your ray to go for distance. So if you already have a distance (from another raycast? I dunno why you would need to do more than one) you would use this.

Vector3.distance(OBJECT.transform.position,hit.point)

OBJECT most likely being to object you are casting from so you can just use transform.position.

Good Luck TMD

Edit. The picture didn't load before. Try Plane.Raycast. Not sure about the arguments.

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 Arnout Swint · May 26, 2011 at 10:41 AM 0
Share

I tried Vector3.Distance, but it gives the distance of ray.origin ins$$anonymous$$d of transform.position. Unity can't seem to mix 2 hitpoints and raycasting.

avatar image
0
Best Answer

Answer by Arnout Swint · May 27, 2011 at 10:05 AM

Got it!

I just made 2 scripts in which 1 calculates the hit.point and gave the distance to the ray2. Now my blue ray (or DVD, lawl) length is the distance between the mouse and the player.

But thanks everyone for thinking ;)

Comment
Add comment · 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
0

Answer by homeros · May 27, 2011 at 07:23 AM

Like said before in the comments you can try finding the closes point of two lines as they'll more likely to get close than to intersect. I've found this script on google search. Maybe it'll help you.

http://paulbourke.net/geometry/lineline3d/calclineline.cs

Comment
Add comment · 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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Raycasting not behaving as expected? 2 Answers

Some raycasts don't hit the object 0 Answers

use the raycast with the rotation 0 Answers

Getting a point on ray 1 Answer

Can anyone help me modify this script? (C#) 2 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