Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I get this script to shoot toward the place where the mouse pointed?

local contextActionService = game:GetService("ContextActionService")


Move = script.Parent
plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
StartTimerValue = 5
MouseLocation = (mouse.Hit.p)


function onKeyPressOne(actionName, userInputState, inputObject)
	if userInputState == Enum.UserInputState.Begin then
			print("2")
			local Part = Instance.new("Part",workspace)
			Part.CFrame = CFrame.new((mouse.Hit.p))
			
		local Ball = Instance.new("Part")
		Ball.Shape = ("Ball")
		Ball.FormFactor = 3 
		Ball.Size = Vector3.new(1, 1, 1)
		Ball.Parent = game.Workspace
		Ball.CanCollide = true
		Ball.Anchored = false
		Ball.Name = ("Ball")
		Ball.Transparency = .8
		Ball.BrickColor = BrickColor.new("Bright green")

		local Fire = Instance.new("Fire")
		Fire.Parent = Ball
		Fire.Color = Color3.new(85/255, 170/255, 0/255)
		Fire.SecondaryColor = Color3.new(170/255, 255/255, 0/255)



		local direction = (plr.Character:WaitForChild("Right Arm").Position).unit 		
		local spawnPos = plr.Character:WaitForChild("Right Arm").Position + direction * 2.0 
		
		Ball.CFrame = CFrame.new(spawnPos, spawnPos - direction)
		Ball.Velocity = direction * 100.0 
		Ball.RotVelocity = Vector3.new(0, 10, 0)
		
		local floatForce = Instance.new("BodyForce") 
		floatForce.force = Vector3.new(0, Ball:GetMass() * 196.1, 0) 
		floatForce.Parent = Ball 
	end
end

game.ContextActionService:BindAction("onKeyPressOne", onKeyPressOne, false, Enum.KeyCode.One)

It'll shoot from the player but once It does it shoots toward the negative Infinity or positive Infinity. How do I make the attack go toward the intended target, the mouse location?

I would be grateful for any assistance.

4 comments

1

Can't you do like Ball.Velocity = Mouse.Origin.p

0

I don't think that's how it works, That's how fast the object is going.

1

http://wiki.roblox.com/index.php?title=API:Class/BasePart/Velocity ~ Description: The speed and movement direction of the object.

0

Oh, My bad! You were right, I'm so sorry XD

1 answer

✓ Accepted answer

direction formula is (target - origin).unit, not just (origin).unit:

local direction = (mouse.Hit.p - plr.Character:WaitForChild("Right Arm").Position).unit

If you want the ball to go in a straight line, you might consider using BodyVelocity instead of BodyForce.

1 comment

0

Oh. Woops o-o'

This discussion is preserved as it was. New answers, comments, and votes are closed.