Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mob = peripheral.wrap("back")
- mob.disableAI()
- target = mob.getMetaByName("yogo_dude") -- Player as the target
- -- Set a block gap distance (how far the Vex should stay behind the player)
- local block_gap = 2 -- You can adjust this to any value you prefer
- local stop_distance = 1 -- Distance within which the Vex should stop moving
- local max_range = 16 -- Maximum range for the Vex to detect the target
- function getRelativePos(target)
- local x, y, z = target.x, target.y, target.z
- local pos = {}
- pos.yaw = math.deg(math.atan2(-x, z))
- pos.pitch = math.deg(-math.atan2(y, math.sqrt(x * x + z * z)))
- pos.dist = math.sqrt(x^2 + y^2 + z^2)
- return pos
- end
- while target do
- target = mob.getMetaByName("yogo_dude") -- Update the player position
- pos = getRelativePos(target)
- -- Ensure the distance is within the sensor's range (16 blocks)
- if pos.dist > max_range then
- -- If the player is too far, stop following
- mob.launch(pos.yaw, pos.pitch, 0)
- else
- -- If the Vex is within range, calculate movement
- -- Adjust the target position to maintain the gap
- local delta_x = target.x
- local delta_y = target.y
- local delta_z = target.z
- -- Calculate relative movement direction (based on the Vex's current position)
- local relative_x = delta_x - me.x
- local relative_y = delta_y - me.y
- local relative_z = delta_z - me.z
- -- Calculate the distance to the player (relative to the Vex)
- local distance = math.sqrt(relative_x^2 + relative_y^2 + relative_z^2)
- -- Adjust the distance to maintain the block gap
- local gap_adjustment_factor = block_gap / distance
- relative_x = relative_x * gap_adjustment_factor
- relative_y = relative_y * gap_adjustment_factor
- relative_z = relative_z * gap_adjustment_factor
- -- Normalize the direction vector (for consistent speed in all directions)
- distance = math.sqrt(relative_x^2 + relative_y^2 + relative_z^2)
- local norm_x = relative_x / distance
- local norm_y = relative_y / distance
- local norm_z = relative_z / distance
- -- Apply movement power to the Vex, with halving the Y axis power
- local power_factor = 0.8 -- Adjust this value for speed control
- local newPowerX = norm_x * power_factor
- local newPowerY = (norm_y * power_factor) / 2 -- Halve the Y axis power
- local newPowerZ = norm_z * power_factor
- -- Calculate the total movement power (magnitude of the movement vector)
- local totalPower = math.sqrt(newPowerX^2 + newPowerY^2 + newPowerZ^2)
- local maxSpeed = 4 -- Maximum speed the Vex can move (adjustable)
- -- Ensure that the Vex doesn't exceed the maximum speed
- local speedScaling = math.min(1, maxSpeed / totalPower)
- newPowerX = newPowerX * speedScaling
- newPowerY = newPowerY * speedScaling
- newPowerZ = newPowerZ * speedScaling
- -- Make the Vex look at the player
- mob.look(pos.yaw, pos.pitch)
- -- If the Vex is too close to the player, it should slow down and stop gradually
- if distance <= stop_distance then
- -- Gradual slowdown: apply a small power if within stop distance
- local slowFactor = math.max(0.2, 1 - (distance / stop_distance)) -- Smooth deceleration
- local smallPower = totalPower * slowFactor -- Reduce the power based on proximity
- -- Launch with reduced power when close, allow it to gradually slow
- mob.launch(pos.yaw, pos.pitch, smallPower)
- elseif distance > block_gap then
- -- Otherwise, apply movement towards the player
- mob.launch(pos.yaw, pos.pitch, totalPower) -- Apply full movement power
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment