CFrame wont look in the correct direction[Solved]

You can write your to pic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am trying to create a CFrame where the mouse touches ingame and with a specific direction to make it easier to manipulate using lookvectors

  1. What is the issue? Include screenshots / videos if possible!

Positioning the CFrame works however whenever I try to rotate the CFrame using a unit vector it doesn’t look in the right direction and barely moves

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using CFrame.Angles, lookat, and others. Im using a part to visualize the cframe’s direction

Here’s my script:

	local shootDir = (mouse.Position - shootPart.Position)		
		warn(shootDir)
		local dirCFrame = CFrame.Angles(math.rad(shootDir.X),math.rad(shootDir.Y),math.rad(shootDir.Z))
		warn(dirCFrame)
		local hitCFrame = CFrame.new(mouse.Position, shootDir) * dirCFrame

	local part = Instance.new("Part")
	part.CanCollide = false
	part.Anchored = true
	part.CFrame = hitCFrame
    part.Size = Vector3.new(0.5,0.5,1)
	part.Parent = workspace.Bullets

And this is the end result:

If anybody has any knowledge on CFrames if I could get some help? Thanks!

1 Like

to find the direction in Vector3, you should use Vector3.Unit. This returns a normalized vector that can be multiplied for longer or shorter distances. To make the part look in the correct direction, you could use CFrame.lookat() and make the part look towards/away from the gun

2 Likes

I have tried Unit, The only problem is I can’t face the part away from the gun because I don’t want it to be facing away from the gun. It’s supposed to point from the front of the gun to where the mouse is which is not exactly away from the gun, I already have the direction for it to point to it just wont point as a CFrame?

Using CFrame.lookat() you can input mouse.Hit.Position as the second arg, it will make the bullet point toward where the mouse is

2 Likes

But the bullet’s position is also mouse.Position so if I make it look at the position it’s already at it just ceases to exist? It’s still in workspace but when I click it it says it’s y position is something like -10000000 or something random and if I press F to find the part the screen just goes black. I need it to look in the direction the bullet is going a CFrame so I can easily create spread by changing the target (hit) Position with lookvectors/rightvectors and vice versa

1 Like

If the bullet is being tweened, the cframe code should be before the tween
Could you send the code so I could have a look?

1 Like

If you meant the little gray part; that’s not part of the game, it’s meant to be a visualization for where “hitCFrame” is and is looking. It’s also not being tweened. If you meant the yellow part then sure

1 Like

change

part.Size = Vector3.new(0.5, 0.5, 1)

to

part.Size *= CFrame.new(0.5, 0.5, 1)

vector3 isnt based part, but cframe is based by part cframe

1 Like

Uhh, what? Im pretty sure you cant change size with CFrame, Vector3 is a 3 sided value where as CFrame is a coordinate frame which where an object is in space, also the size of the temporary part has nothing to do with my problem? I just want the CFrame(hitCFrame) To point in the direction the bullet is coming from.

1 Like
local hitCFrame = CFrame.new(mouse.Position, shootDir) * dirCFrame

The second argument in CFrame.new() is treated as a position vector, not as a direction vector, in order to solve this you have to add mouse.Position to it:

local hitCFrame = CFrame.lookAt(mouse.Position, mouse.Position + shootDir) -- I removed "* dirCFrame" because it's unnecessary considering what you're trying to achieve

Please let me know if I misunderstood your post or if you’re still encountering errors.

3 Likes

Thanks! This worked I really appreciate the help from you and everyone else!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.