Hello devforum,
I’ve created a (rather retro) gun that shoots parts towards the cursor, but it seems that instead of going towards the cursor, the maxforce of the bodyvelocity causes the forces to conflict and fight each other, leading to certain positions where the bullet goes flying all over the place or nowhere
A DEMONSTRATION:
robloxapp-20200520-0948202.wmv (4.2 MB)
MY CODE:
local tool = script.Parent
local sound = script.Parent.Sound
game:GetService("ReplicatedScriptService")
local shootevent = game.ReplicatedStorage:WaitForChild("ShootEvent")
local function serverfired(LOC, LOC2)
local playerworkspace = workspace:FindFirstChild(tostring(LOC))
local bullet = Instance.new("Part", workspace)
bullet.Shape = "Ball"
bullet.Position = playerworkspace.PelletGun.Handle.Position
bullet.CanCollide = false
bullet.Size = Vector3.new(.5, .5, .5)
local bodyposition = Instance.new('BodyPosition', bullet)
bodyposition.MaxForce = Vector3.new(100, 85,100 )
bodyposition.P = 1000
bodyposition.Position = LOC2
end
shootevent.onServerEvent:Connect(serverfired)
If you’re trying to propel a projectile towards a given target, I believe the usage of the RocketPropulsion BodyMover would suit your needs more. The article on the linked page can assist in understanding how it works.
If you insist on using BodyPosition, I don’t see any specific reason as to why this works as described and not as intended. Perhaps you could try tweaking the BodyPosition.D
property to adjust the dampening, but I am unsure if that would help.
1 Like
Currently trying out your solution; to overcome the need for propelling it towards a part instead of a Vector3, i’m implementing a script that adds a part whose position will update itself so that it is always near the mousecursor, although if there is a way to propel a part towards vector3 instead of a target, i’m all-ears
New problem: I have implemented the cursor script, cutting out the live position update with creating a new part at the mouse cursor; the part doesn’t go anywhere
robloxapp-20200520-1052437.wmv (1.6 MB)
UPDATED CODE:
local function serverfired(LOC, LOC2)
local playerworkspace = workspace:FindFirstChild(tostring(LOC))
local cursor = Instance.new("Part", workspace)
cursor.Position = LOC2
cursor.Size = Vector3.new(.1, .1, .1)
local bullet = Instance.new("Part", workspace)
bullet.Shape = "Ball"
bullet.Position = playerworkspace.PelletGun.Handle.Position
bullet.CanCollide = false
bullet.Size = Vector3.new(.5, .5, .5)
local rocketpropulsion = Instance.new("RocketPropulsion", bullet)
rocketpropulsion.Target = cursor
rocketpropulsion.MaxSpeed = 100
end
I see no reason as to why this fails, other than perhaps the RocketPropulsion.MaxThrust property not being set and (maybe?) defaulting to 0? I’m afraid I can’t offer any help in this case. Perhaps someone else could assist, I’m watching this thread as I’d like to know how this would be worked out.
1 Like
EUREKA!!
It turns out that i’ve been using the wrong bodymover; If you use bodythrust and set the force to mouse position
local bodythrust = Instance.new('BodyThrust', bullet)
bodythrust.Force = LOC2 -- Where LOC2 = mouse.hit.p
The part moves precisely to where you want it to:
robloxapp-20200520-1147511.wmv (1.4 MB)
CAVEAT:
You MUST have mouse.hit ignore everything in the workspace, like so:
local mouse = player:GetMouse()
mouse.TargetFilter = workspace
If you don’t do this, the bullet will not have enough force to go anywhere
Another piece of information to jot down:
An excerpt from the wiki might explain why rocketpropulsion wasn’t working earlier:
RocketPropulsion.CartoonFactor
is how much the part points towards its target. 0 is pointing straight up away from the target, 1 is directly at the target. The default is 0.7 in order to counter the force of gravity.