Alternative to using playerpart.lookVector

  1. What do you want to achieve?
    I want to make smooth throwing, in the direction that the player is facing.

  2. What is the issue?
    I noticed the animations are offsetting the throws. How am I supposed to make a consistent straight throw if I’m throwing it relative to the playermodel?

  3. What solutions have you tried so far?
    The developer forum didn’t seem to have anything related to my issue.
    EDIT: I tried using a region3 to create a part but I was unsure of how to rotate it in the direction of the player. I’ve also tried using the HumanoidRootPart, but it seems to offset.

Anything helps!

An easy long-term solve would just be to simply remove external animations in your game. Or, alternatively, you could draw a bodygyro from the player to the cursor position, and have the throw relative to the cursor position, so that the animations do not get in the way.

1 Like

Thank you for trying to help, but I specifically want to use keys when throwing. I resolved the ball having a “floating” effect.

But throwing without offset has proved an extremely challenging task. I’ve also tried making a Region3 relative to the players position and throw relative to the direction of the player, but I can’t seem to figure out how to make the part rotate to the direction of the player.

I thought of maybe using the x direction the camera is facing, similar to a compass. But what if the player turns their camera to the side… It would move in that direction.

If anyone else could provide a possible solution I would be extremely greatful!

I’m not sure if I understood your problem properly. If you’re using the LookVector of some of the parts in the character for getting the direction the ball needs to go, which part are you using? Some of the visible bodyparts or the invisible HumanoidRootPart?. If you don’t want animations to affect the direction, you can use the LookVector of the HumanoidRootPart.

2 Likes

Using the HumanoidRootPart like @RoBoPoJu stated is a good idea and always what you should do in this scenario (animations messing up character rotation), since it is always a neutral description of where a character is and where it is facing. However, in this case, you might want to use the camera’s LookVector, since it’s more intuitive to throw an object where you’re looking at, not where your character is facing.

1 Like

The issue is, I did use the HumanoidRootPart (I edited the post sorry.), I believe it animates with the player as well because the animations seem to offset it each time, even when I put consistent values for the throws.

EDIT: I also specified why I didn’t want the throwing relative to the camera.

Sorry if I wasn’t being clear, but let me give an example that could possibly clear things up for you.

Lets say we have a head, and since I own the cartoony animation, the head bobs to the side every once in a while, If I decided to throw with the lookVector of the head, It would throw in the direction that the head bobbed in. Even if I use the torso there still is some offset. As you saw in the video, to replicate some sort of “breathing” like animations they move the torso as well.

I found an issue within my localscript that seemed to have offset it aswell. Another issue is that even though I put the Velocity.Y to 1000 it doesn’t rise. I’m going to mark your post as the solution as soon as I solve this.

If you haven’t solved the issue that the ball isn’t rising, I think I know the reason and a solution to that. In the line of code you put in the reply to emojipasta, you multiplied the LookVector with Vector3.new(300, 1000, 300). When calculating the velocity this way, the x and z coordinates will be what you want them to be, but if you want the ball to go higher than the LookVector of the HumanoidRootPart, it won’t work.

Here’s the reason: When the character is upright, the Y value in the LookVector of the HumanoidRootPart is always 0. When you multiply the LookVector with the other Vector, you get a Vector3 where

X = LookVector.X*300, Y = LookVector.Y*1000, Z = LookVector.Z*300

Because LookVector.Y is 0, the Y in your Velocity Vector is 0*1000 which is 0.

Instead of calculating the Velocity that way, maybe do it like this:

local SPEED = 300 -- how many studs the ball moves in one second
local ANGLE_IN_DEGREES = 40 -- How many degrees you want it to rise

-- Roblox functions use radians instead of degrees, 
-- so degrees need to be converted to radians for the calculations
local angle = math.rad(ANGLE_IN_DEGREES)

local lookVector = player.Character.HumanoidRootPart.CFrame.LookVector

-- the ratio of the calculated x and z will be the same as the ratio of these,
-- so the horizontal direction will be the same as the direction of the HumanoidRootPart
local lookX, lookZ = lookVector.X, lookVector.Z

-- these are used to turn the x and/or z to negative
-- if the corresponding lookVector coordinate is negative
local xm, zm = lookX < 0 and -1 or 1, lookZ < 0 and -1 or 1

-- turn these into their absolute values to make the calculations wont give any NAN values
lookX, lookZ = math.abs(lookVector.X), math.abs(lookVector.Z)

-- calculating the new direction values
local newX, newY, newZ
if lookX == 0 or lookZ == 0 then
	if lookX == 0 then
		newX = 0
		newZ = math.cos(angle)
	else newX = math.cos(angle)
		newZ = 0
	end
	newY = math.sin(angle)
else newX = math.abs((math.cos(angle)*lookX)/math.sqrt(lookX^2+lookZ^2))
	newZ = math.abs(lookZ/lookX*newX)
	newY = math.tan(angle)*math.sqrt(newX^2+newZ^2)
end

-- make the values negative if the corresponding coordinate is negative in the lookVector
newX, newZ = newX*xm, newZ*zm

-- This is a unit Vector, like the LookVector. Its magnitude(length) is almost exactly one
-- (just little inaccuracy)
local dir = Vector3.new(newX, newY, newZ)

-- setting the velocity of BV
BV.Velocity = dir*SPEED

This can be achieved more easily with CFrame multiplication, but I thought that it might be more efficient when using the code above. I’m not sure about the efficiency, though, and the performance impact will be extremely small even if there is one. Anyways, here’s an alternative with CFrame multiplication. It’s a more readable option so it might be better to use it, because an extremely small possible efficiency bonus might not be worth sacrifising readability.

local VELOCITY = 300
local ANGLE_IN_DEGREES = 40

local dir = (player.Character.HumanoidRootPart.CFrame*CFrame.Angles(math.rad(ANGLE_IN_DEGREES), 0, 0)).LookVector
BV.Velocity = dir*SPEED

The solution to the going up problem was simply this;

BV.Velocity = BV.Velocity + Vector3.new(0,  upPower, 0)

Thanks for the help though I marked you for solution :slight_smile: