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: Harm the player if they look at a brick

Previous Thread :: Next Thread 
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
06 Apr 2014 02:57 AM
How could this be done? Raycasting I'm guessing?
Report Abuse
fahmisack123 is not online. fahmisack123
Joined: 04 Jan 2013
Total Posts: 581
06 Apr 2014 03:10 AM
I don't think there's a looking event.
Report Abuse
vlekje513 is not online. vlekje513
Joined: 28 Dec 2010
Total Posts: 9057
06 Apr 2014 03:15 AM
I think lookvector or atan2.
Report Abuse
Cr0ws is not online. Cr0ws
Joined: 16 Aug 2013
Total Posts: 1545
06 Apr 2014 04:25 AM
whats atan2?
Report Abuse
Voidition is not online. Voidition
Joined: 14 Jul 2012
Total Posts: 1849
06 Apr 2014 06:13 AM
[ Content Deleted ]
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
06 Apr 2014 07:16 AM
atan2 is like a corrected atan; it can calculate in four quadrants, instead of just 2.

Now, ray casting wouldn't exactly work, and it would just be better to use a dot product to find the angle between 2 vectors that we know.

One of these vectors represents where the direction the player is looking, so let's use the .lookVector of his head (imagine a line pointing forward out from the head).

The other vector we need is the direction between the player and the brick (as a unit vector).

Here's a diagram I drew in MS Paint http://puu.sh/7Y6fw.png , where θ is the angle between the vectors.

Given that V1 and V2 are the vectors (doesn't matter which is which) the formula for working out theta is as follows:

local cos = V1.x * V2.x + V1.y * V2.y + V1.z * V2.z
angle = math.deg(math.acos(cos))

The angle will be between 0 and 180: 0 represents the player looking directly at the block, while 180 represents the player looking in the dead opposite direction.

You can then compare this angle, and harm the player if they are looking within a certain threshold, like so

if angle <= 30 then
--harm player
end

Hope that's helpful
Report Abuse
magicalandre13 is not online. magicalandre13
Joined: 21 Jan 2011
Total Posts: 5327
06 Apr 2014 07:51 AM
find the torso position?
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
06 Apr 2014 03:38 PM
@RoflBread; Yeah, that sounds useful if I was good enough to understand it..

But the game I'd use it in forces you to be in first person, so I was thinking maybe a "rig" that welds parts facing out from your face/torso, if the brick touches the rig, it harms you.
Report Abuse
Azureous is not online. Azureous
Joined: 29 Jan 2012
Total Posts: 25287
06 Apr 2014 03:44 PM
lookVector?
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
06 Apr 2014 03:57 PM
@Kinnis,

The way I posted would still work with first person, but your way could work too. Up to you :P
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
06 Apr 2014 04:01 PM
I would not mind writing it out for you, either :-)
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
06 Apr 2014 05:14 PM
I normally try not to ask people to write script for me, but this is a bit important, so if you want you can.
Let's name the brick "body" Alright? Alright :>
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
06 Apr 2014 05:18 PM
Also it needs to constantly harm them as long as they're looking at it. If they're looking at it their health slowly drains.
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
06 Apr 2014 11:58 PM
Also, the brick has to be actually visible. If a brick is between them blocking the view it's fine.

This is why I think it's only possible by raycasting.
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
06 Apr 2014 11:59 PM
Have a GUI that points a constant ray in whatever direction the player looks.
The ray is a cylinder large in width.
If the ray touches the brick damage is dealt.

Not sure how to make it drain the health rather than do damage per touch though.
Report Abuse
islandmaker2012 is not online. islandmaker2012
Joined: 07 Nov 2012
Total Posts: 9327
07 Apr 2014 12:15 AM
Kinn..

Ray.new

doesn't create a physical object..
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
07 Apr 2014 12:17 AM
o

I've never used them.

But the rig idea still stands possible.
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
07 Apr 2014 03:34 AM
Bump.
I'd need help making the rig too.
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
07 Apr 2014 06:50 AM
In a local script in StarterGui

p = game.Players.LocalPlayer
c = p.Character
target = Workspace.THE_HARM_BRICK --
camera = Workspace.CurrentCamera

function raycast()
local dir = camera.CoordinateFrame.lookVector
local ray = Ray.new(c.Head.Position, dir*999)
local hit, pos = Workspace:FindPartOnRay(ray, c)
if hit == target then
c.Humanoid:TakeDamage(1)
end
end


while wait() do
raycast()
end

But that only harms them if are looking right at it (cursor on it in 1st person).
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
07 Apr 2014 07:34 AM
K this got complicated fast, haha. Spent a while making this, see what you think:

http://pastebin.com/UxhvFwvz

Still in a local script in startergui or wherever. This script assumes the kill brick is a single part, not a model.
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
07 Apr 2014 07:45 AM
The script won't harm you if there is something between you and the brick; and it takes into account your field of view instead of just damaging you when a single ray hits the harm brick.
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
07 Apr 2014 08:40 AM
Oh wow, that works amazingly actually!
Though it works better with an angle of < 60 but I edited it.
Report Abuse
RoflBread is not online. RoflBread
Joined: 18 Jun 2009
Total Posts: 3803
07 Apr 2014 08:43 AM
Great! Glad I could help.
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
07 Apr 2014 08:48 AM
Thanks mate, I'll definitely give you credit for anything I use it in.
Report Abuse
Kinnis97 is not online. Kinnis97
Joined: 21 Feb 2010
Total Posts: 1683
07 Apr 2014 10:08 PM
I have the target set to a brick inside a model, and the bricks's kind of merged with other bricks. (Inside each other.)
It now only harms the player if they look directly at it, and/or if they're close to it. It's really vague and the problem changes slightly every time I try to fix it.
I tried making the target a lone brick in the Workspace but it still doesn't work right.
Maybe it's the place, or the fact that it's being cloned into the player's PlayerGui, not placed in the StartGui.
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