[CLOSED] How to rotate bullet to face target

I am trying to make a turret. It shoots fine and everything but my issue is that no matter where you are shooting the bullet comes out of the barrel at the same angle (see this link to see what I mean, beginning of the video shows it firing downwards at the wrong angle, end of the video shows it firing up at the correct angle.). I am wondering how to rotate the bullet so it isn’t the same angle regardless of where it is being shot.

Bullet firing code:

local Bullet = Start:Clone()
local Top = Bullet.Top
local Body = Bullet.Body
local antiGravity = Instance.new("BodyForce")
antiGravity.Force = Vector3.new(0, workspace.Gravity * Body:GetMass(), 0)
antiGravity.Parent = Body
Bullet.Name = "Bullet"
Bullet.Parent = BulletsFolder 
Body.Velocity = CFrame.new(Player.Character.HumanoidRootPart.Position, End).lookVector * 100
Body.Anchored = false
for i, obj in pairs(Bullet:GetChildren()) do
	if obj:IsA("Part") then
		obj.Transparency = 0
		if obj.Name == "Top" then
			obj.CanCollide = true
		end
	end
end
Top.Touched:Connect(function(Hit)
	if not Hit:IsDescendantOf(Player) and not Hit:IsDescendantOf(Start.Parent) then
		local explosion = Instance.new("Explosion")
		explosion.DestroyJointRadiusPercent = 0
		explosion.Parent = workspace.Terrain
		explosion.Position = Vector3.new(Top.Position.x,Top.Position.y,Top.Position.z)
		Bullet:Destroy()
		wait(0.5)
		explosion:Destroy()
	end
end)
1 Like
local pos = Bullet:GetPrimaryPartCFrame().Position
Bullet:SetPrimaryPartCFrame(CFrame.lookAt(pos, End))

Multiply by 90 degrees or whatever depending on your exact setup.

3 Likes

Can you explain what you mean by this?

1 Like

What you’d do is essentially change the Orientation of the bullet to match that of the mouse position through CFrame. To do that, you should know (if you don’t already) that a basic CFrame constructor is

CFrame.new(vec3pos, vec3lookAt)

And so to make your bullets orient in the right direction, you need to have a CFrame constructor that keeps their position but changes their orientation. You can do that like this (forgive me, I have little experience with models)

Note: I am putting “Body” because I believe that’s the primary part of the bullet, but if it isn’t then change it (the word) to the primary part.

local mouse = player:GetMouse()

Body.CFrame = CFrame.new(Body.Position, mouse.Hit.p)

If the script isn’t local and you don’t have access to the player’s mouse, then send the value of mouse.Hit.p through a remote event.

1 Like

This is essentially what @nicemike40 was telling me to do. Your code and theirs both cause the bullets to point downwards. However, they do slightly rotate if you go higher (which this is the goal but the rotation needs to be more amplified). The reason I was asking him to clarify what he meant when he said

is because I believe if I figure out how to do the multiplication he is suggesting it will fix the bullets facing downwards.

1 Like

Oh okay, then it’s probably because the LookVector of the bullet’s body is the wrong face. If you can get the orientation of the bullet when it’s shot you can, like mike said, multiply by 90 degrees on the axis that’ll tilt it up during the creation at the top of the script

2 Likes

Are they always downwards or is there a little tilt?
Probably due to your “bullet” hitbox you can’t truely get it accurate the way they’re suggesting you to do it. Is this a part/union/model?

3 Likes

It is four parts inside of one model.

1 Like

The axis is the z axis so I will try to mess with that.

2 Likes

I already expected that lol, that means it does infact have a messy hitbox, if so then I will assume the “bullet” to already be welded and have a PrimaryPart, if it does not have a primary part I suggest you create a part in the centre of the model and weld everthing using weldconstraints to then what you can actually do is rotate the primarypart instead (if you have done that then remember to select that part as primarypart by going to bullet model properties).

By doing

local Position = Bullet:GetPrimaryPartCFrame().Position
local lookAt = --Here get the position you want the part to look at, i'm unsure how you fire a bullet but this should be position the bullet is going
Bullet:SetPrimaryPartCFrame(CFrame.lookAt(Position, lookAt))
1 Like

I just meant that you might need to add

* CFrame.new(math.pi / 2, 0, 0)

to the end of the CFrame. Might need to pick a different axis or negate it, depending on which axis is “forward” on the bullet.

1 Like