In my game, I’m using the Unit of a Vector3 to show the visual aspect of a bullet. When I click far into the distance, the unit has a few numbers slightly off from the original distance, and when I multiply it again, it offsets the bullet noticeably. Although this doesn’t affect the actual function of the game, I find it really unpleasing to see a bullet land a centimeter off from where I clicked.
example code:
local GoalPositionPreUnit = (mousePosition + (newBeam.CFrame.LookVector * 3))
local GoalPositionUnit = GoalPositionPreUnit.Unit
local GoalPosition = GoalPositionUnit * 100
I have it print the original position and the .Unit version:
Original Position: 6290.39453125, 1743.1947021484375, -7594.9912109375
.Unit: 0.6281240582466125, 0.17406579852104187, -0.7583938837051392
it ever so slightly changes, but when it is recalculated, it makes a significant visual difference.
Are there any work arounds to this issue / errors on my part causing it? If so, let me know and thank you.
Please correct me if I’m wrong, but I’m sure that .Unit is not actually at fault here. It seems to be doing exactly what it is supposed to do.
Take a look at the following implementation of .Unit that I quickly coded up:
local function unit(vector: Vector3): Vector3
local length = ((vector.X)^2 + (vector.Y)^2 + (vector.Z)^2)^0.5
return vector / length
end
This is by definition what .Unit does: It normalizes a vector, which means that it returns a vector with the same direction as the given vector but with length 1.
These are the values I get using your example vector:
…which seem like a one-to-one match to .Unit’s results. A calculator also confirms this result, so it doesn’t seem to be a roblox-sided calculation error either. This is backed-up by the fact that multiplying the result from unit() by the original vector’s length gives you the original vector, which only makes sense.
I could be totally wrong here, but it doesn’t seem like .Unit is the problem here. Have you checked for other possible issues?
I’m very sorry, I think this is above my pay grade. I don’t have much experience with CFrames. From what I’ve looked at though, I’d assume that this might be the issue:
local GoalPosition = (mousePos + (newBeam.CFrame.LookVector * 3)).Unit * 100
I’ve understood the lines before it and I think those aren’t causing the issues, but what is this supposed to do? Isn’t the goal position the mouse’s position? You’re basically adding wherever the newBeam is looking at to the goal’s position, if I understand correctly, which would mean that when you’re looking somewhere specific, it offsets the goal by some amount.
But I’m not sure - my first time actually looking at CFrame’s documentation would be 15 minutes ago, therefore I’d leave this for someone experienced with CFrames.
It’s okay, you’ve helped me enough so far. Thank you. I believe that could be the issue as well, I’ll look into it and give you a solution if it’s the root of the problem. Thank you for everything.