Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Use Raycast or Collisions?

Asked by 6 years ago

In a game with many zombies, should I find and damage the player by using raycast(from zombie torso to lookVector) or by Touched() event?

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

For my zombies i check to see if the player is within a certain distance then a run an attack function

local dist = zombie.HumanoidRootPart.Position - closestPlayer.Character.HumanoidRootPart.Position).magnitude -- gets the distance between the zombie and the player
if dist <= 15 then
    if canAttack then -- if the zombie can attack
        canAttack = false
        Attack() -- your attack function        
        wait(attackCooldown)
        canAttack = true
    end
end

You just need to update this every second or when an event fires for it to work.

0
I think this will put too much load on the game. I want to know the best and most efficient way of making a zombie attack a player. Francisl4d45 7 — 6y
0
nice place this under Humanoid.Running event awesomeipod 607 — 6y
0
@Francis I think his method is better than using touched event because touched interacts with all parts. So if the zombie is walking the legs will touch the ground firing the event constantly. awesomeipod 607 — 6y
0
^You don't necessarily need to run the Touched event on all parts? Touched is very efficient but not always the most reliable...if your zombie is good at tracking then it should work fine Vulkarin 581 — 6y
0
You need to run is on all the parts because if only the HRP or Torso is using Touched, the player can just jump on top of the zombie's head and not take any damage. awesomeipod 607 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

If you want is to count if the zombie touches the player, use :Touched().

If this helped you, make sure to accept the answer, it will help us both out.

0
This did not help, sorry. You misunderstood my question. I was asking what is better to use - Touched() or Raycast to check if player is right next to the zombie, so that the zombie could do damage. Francisl4d45 7 — 6y
0
Player touching Zombie or Zombie touching Player, same thing... makes no difference. AlphaGamer150 101 — 6y

Answer this question