generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: .unit in raycasting

Previous Thread :: Next Thread 
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:39 PM
I'm looking at code from the wiki, I looked at several sources, and I couldn't find what the .unit means.

Here's example code from the wiki:

local ray = Ray.new(
Weapon.Handle.CFrame.p, -- origin
(mouse.Hit.p - Weapon.Handle.CFrame.p).unit * 500 -- direction
)
local ignore = game.Players.LocalPlayer.Character

local hit, position = Workspace:FindPartOnRay(ray, ignore)

On line 3, is where there is .unit. Everything else I understand fine.
Report Abuse
lolb3 is not online. lolb3
Joined: 16 Jan 2010
Total Posts: 2268
01 Jan 2014 07:42 PM
transforms your cframe/vector3 into code that your roblox client can read and direct the ray towards
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:45 PM
But the first part didn't need the .unit after it.
Report Abuse
johnspartan117 is not online. johnspartan117
Joined: 01 Jan 2008
Total Posts: 3124
01 Jan 2014 07:46 PM
Off-topic and not helping, but what is "raycasting?"
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:52 PM
This is not at all off topic. Rays are invisible lines that are created from 1 point to another relative to the first point. They're mainly used to look for blocks in the way of something.
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:53 PM
Oh I'm sorry I thought you were saying my thread was off-topic. Sorry about that.
Report Abuse
johnspartan117 is not online. johnspartan117
Joined: 01 Jan 2008
Total Posts: 3124
01 Jan 2014 07:54 PM
This could be implemented into a BOT (Zombie for ex.) and avoid bricks that are like three studs away and rotate to move someplace else? Kind of grasping this idea.
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:57 PM
No when you're programming AI you use a path finder algorithm. Using rays for that would work theoretically but it would be inefficient, very inefficient. I'm using it to make a laser just as an exersize, I need to make sure I grasp this concept completely.
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:57 PM
So does anyone know what .unit does?
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 07:58 PM
And why it is there. What importance does it have?
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
01 Jan 2014 08:01 PM
Mmmm, how can I explain this?

.unit basically gives it the length of 1 (stud). Since we know this, we can multiply it by whatever to make a ray however many studs we want.
Report Abuse
johnspartan117 is not online. johnspartan117
Joined: 01 Jan 2008
Total Posts: 3124
01 Jan 2014 08:03 PM
Perhaps the base of the beam? I wouldn't understand where the 500 comes fro though... Hmm...


Algorithms you say? YUCK!
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 08:05 PM
(mouse.Hit.p - Weapon.Handle.CFrame.p).unit * 500 -- direction
)

I guess that would make more sense for why the first argument doesn't have .unit. So basically it's giving a limit to how far the ray will travel. Will, first off the limit is 1000 by default and can't be raised. So, it's like giving how far it can travel for range.
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 08:06 PM
So EvolvedPikachu, the first argument is the origin, and the second is the mouse's position - the origin to make it the distance from the origin. But what you're saying is that we can limit the distance instance of making it however far the mouse was? And the limit in this case is 500 studs?
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
01 Jan 2014 08:06 PM
yup

If you wanna go above 1000 studs then just use multiple rays.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
01 Jan 2014 08:07 PM
I was replying to the reply above the reply above my reply.

that reply above my reply idk but if you wanna limit then yeah I guess
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 08:07 PM
Is that a yes to the second post too? Also multiplying rays seems difficult. Would you just give it the second one the same direction and make the origin the second argument from the first?
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 08:08 PM
So wouldn't that make .unit optional or can I not subtract the two vectors?
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
01 Jan 2014 08:09 PM
You wanna go > 1K studs? Do something like

part1,pos1 = findpartonraymethodwhatever
extendpart,extendedpos = Workspace:FindPartOnRay(Ray.new(pos1,direction),whatever,whatever)
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 08:11 PM
Okay.

So where'd you find out that .unit just makes it one stud?
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
01 Jan 2014 08:12 PM
.unit is optional, but then the ray isn't even with itself anymore and that's not good.
Report Abuse
evolvedpikachu is not online. evolvedpikachu
Joined: 18 Aug 2010
Total Posts: 10696
01 Jan 2014 08:12 PM
google obviously lelelelelel

No, I'm serious, I looked up what a unit vector was a skimmed the descs.
Report Abuse
rosenburger is not online. rosenburger
Joined: 03 Aug 2009
Total Posts: 8407
01 Jan 2014 08:13 PM
Okay..
Report Abuse
cntkillme is not online. cntkillme
Joined: 07 Apr 2008
Total Posts: 44956
02 Jan 2014 12:33 AM
I don't know if your question was answered but all .Unit does is make the vector normalized (making the length of the Vector equal 1) so that all components squared then added up will equal 1
Report Abuse
lombardo2 is not online. lombardo2
Joined: 30 Nov 2008
Total Posts: 1604
02 Jan 2014 12:45 AM
Yeah, he already knows that. That's not what he's asking, he's asking why it is needed.

It's needed to specify the length of the ray to be tested, rays are infinite, they start at a point but doesn't end. If FindPartOnRay tried to check the whole ray for collision it would never end. So FindPartOnRay interprets the Ray's direction vector length as the distance, in studs, it is going to test for collision. So we do (lookAt - start).unit to normalize the vector so then we can multiply it by a number to control the distance FindPartOnRay will work on.
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image