If it’s physics simulated, you have to apply a force correcting the path, which is usually perpendicular to the velocity.
local planeN = mouseDir.Unit:Cross(velocity.Unit)
local sideVec = velocity.Unit:Cross(planeN)
VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
VectorForce.Force = k * sideVec -- k being a proportional multiplier
function rotateVectorAround( v, amount, axis )
return CFrame.fromAxisAngle(axis, amount):VectorToWorldSpace(v)
end
Example use:
Script
local VERTICAL = Vector3.new(0, 1, 0)
local HORIZONTAL = Vector3.new(1, 0, 1)
local rotateSpeed = 1
local followSpeed = 20
local bullet = script.Parent.Bullet
local target = script.Parent.Target
function rotateVectorAround( v, amount, axis )
return CFrame.fromAxisAngle(axis, amount):VectorToWorldSpace(v)
end
--Skip the first heartbeat because it's dt is too high because it waits for game to load
game:GetService("RunService").Heartbeat:Wait()
game:GetService("RunService").Heartbeat:Connect(function(dt)
local dirToTarget = bullet.CFrame:VectorToObjectSpace((target.Position - bullet.Position))
local angleToTarget = math.atan2(-dirToTarget.X, -dirToTarget.Z)
local rotateDir = (angleToTarget < 0 and -1) or (angleToTarget > 0 and 1) or (0)
local rotateAmount = math.clamp(rotateDir * rotateSpeed * dt, -math.abs(angleToTarget), math.abs(angleToTarget))
bullet.CFrame = CFrame.new(
bullet.Position,
bullet.Position + rotateVectorAround(bullet.CFrame.LookVector, rotateAmount, VERTICAL)
) * CFrame.new( Vector3.FromNormalId(Enum.NormalId.Front) * followSpeed * dt )
end)
I believe what hes trying to do is create a bullet which upon being fired can alter its trajectory and curve towards the mouse’s current position. So upon firing the bullets, the bullet can then be guided by the cursor towards its target much like how a laser guidance works in real life.
I do not however, have much experience in this. But on a physics basis, it would be applying a counter force (with a maximum force I assume so that it doesn’t just immediately switch) in order to correct the current trajectory towards the new trajectory. He hasn’t however, specified how he wants the trajectory to be corrected.
here the actual game im using it for, it kinda works right now but due to my current code it doesn’t work well when shooting directly at something
My current solution im trying to do (at this moment) is to cross product the velocity vector and mouseposition vector to get a perpdiencular vector and then rotating that matrix with cframe axisangle to get the new velocity vector
blue vector = velocity
green vector = desired velocity
pink vector = mouseposition
red vector = cross product vector
Edited : just saw @ThanksRoBama solution and that is esstinally what I believe my solution is what I am trying
I did it a bit different from the way you did it but I still used your function
local Step = wait()
local MouseOrigin = Mouse.UnitRay.Origin
local MouseDirection = Mouse.UnitRay.Direction
local MouseProjection = MouseOrigin + MouseDirection*(MouseDirection:Dot(Part.Position-MouseOrigin) + Data.Speed*Step*4)
local CrossVector = PartVelocity:Cross(MouseProjection-Part.Position)
local UnitCross = CrossVector.Unit
if UnitCross == ZeroVector then continue end
local Angle = math.acos(PartVelocity.Unit:Dot( (MouseProjection-Part.Position).Unit ))
if Angle ~= Angle then continue end
local RotateAmount = math.min(RotateSpeed*Step,Angle)
local NewVelocity = RotateVectorAround(BodyVelocity.Velocity/Data.Speed,RotateAmount,UnitCross)
BodyVelocity.Velocity = NewVelocity.Unit*Data.Speed
PartVelocity = BodyVelocity.Velocity