I am making a pseudo tower defence game.
I have this tower, I want it to attack any enemy NPC if it’s within range.
I know I would use Cframes to rotate the turret to the face the NPC.
Raycasting is no problem.
Though I still do not know how I would make a range.
In this case, it’s a bit complicated, I’m trying to use Egomouses placement system and I also want the cone-shaped range to be displayed for the player to see when it is in placement mode.
Should I use an invisible cone-shaped part or something in front of the turret? and it will only show and glow when it is being placed? That is one solution I am considering though I find it a bit impractical.
I’m not looking for someone to script for me, I just want advice on what I use in order to overcome this problem.
You can use CFrame:PointToObjectSpace(
Target’s position), it will use the orientation of the CFrame and return the a vector3 of the target as observed by the origin CFrame.
Basically, it returns how far the target is from where the CFrame is looking.
You can then use some trigo to derive the angle of the target from the normal and determine whether to activate the turret.
If you need clarification feel free.
Yep to add on here is an example of CFrame point to object space to measure the yaw angle and rotate a turret accordingly:
Here’s what I’ve come up with, it only rotates on the yaw axis, I was thinking it’d be interesting to have yaw and pitch be different “hinges” / Motor6Ds:
TurretControllerlocal RunS = game:GetService("RunService")
local PIDController = require(game.ServerStorage.PIDController)
local turret = script.Parent
local turretBase = turret.Base
local yawBase = turret.YawAssembly.YawBase
local yawMotor = turret.Base.YawMotor6D
The turret should first aim at the enemy which fits the restricted yaw angle criteria and is closest to the turret which you can measure using magnitude between position of the enemy and turret.
Also for moving the turret with CFrames I made this, which should simplify the process.
Hello there, it’s me again with a small resource that I see lacking on the forum with
many, unsolved, or ones that have the general idea but incomplete scripting support questions.
@sleitnick, PID Controller from AeroGameFramework, works great but semi modified to accommodate Vector3 variables.
@ThanksRoBama, Great tutorial on PID Controllers which had excellent yet simple explanation along with great examples. Never knew it was possible to just work with P alone.
This seems like a great solution.
I’ll try something out and will ask if I run into something.
That resource seems extremely useful.
Thanks for sharing.