# Raycast stop at distance

 0 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: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 :) more ▼ asked May 26 '11 at 09:37 AM Arnout Swint 48 ● 2 ● 2 ● 8 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. May 26 '11 at 10:38 AM Bunny83 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 May 26 '11 at 10:42 AM Bunny83 My first ray = Camera.main.ScreenPointToRay(Input.mousePosition). I want the ray2 length to be from transform.position (or ray2.origin) to hit.point. May 27 '11 at 06:49 AM Arnout Swint add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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 ;) more ▼ answered May 27 '11 at 10:05 AM Arnout Swint 48 ● 2 ● 2 ● 8 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 try usingPhysics.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 TMDEdit. The picture didn't load before. Try Plane.Raycast. Not sure about the arguments. more ▼ answered May 26 '11 at 09:50 AM themusicdork 46 ● 3 ● 3 ● 5 I tried Vector3.Distance, but it gives the distance of ray.origin instead of transform.position. Unity can't seem to mix 2 hitpoints and raycasting. May 26 '11 at 10:41 AM Arnout Swint add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 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 more ▼ answered May 27 '11 at 07:23 AM homeros 321 ● 1 ● 2 ● 6 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x1947
x1578
x371
x194
x188

asked: May 26 '11 at 09:37 AM

Seen: 1439 times

Last Updated: May 27 '11 at 10:05 AM