Unable to run multiple Vectors

I can’t put this in words, but the only thing occuring when running this statement:

repeat wait()
	nearestObject.PrimaryPart.Velocity = player.Character.Head.CFrame.LookVector * 50
	nearestObject.PrimaryPart.Velocity = player.Character.Head.CFrame.UpVector * Vector3.new(0,44,0)
until nearestObject.PrimaryPart.Velocity == player.Character.Head.CFrame.UpVector * Vector3.new(0,44,0)

Is the UpVector, probably because it’s ahead of the LookVector. I have a picture on what it should look like.

Screenshot 2020-11-26 131540
The green line is suppose to represent the wanted outcome, the yellow line just represents the UpVector, and the orange one at the bottom shows what it would look like with just the LookVector. So basically, I just want it to “merge”.

Note: This isn’t the exact script formatted into DevForums!

Setting the velocity like that wont work as intended. The lookVector will return a CFrame that is relative to the part.

So if you were to be at the position {100000,50,0}
The force applied would be massive as it is setting the lookvector to be the velocity.

– You would want to use the difference instead to stop this issue.
– The other issue is the until statement. For the statement to end the Velocity would have to match exactly which isn’t likely.

1 Like

If I’d remove that repeat loop, one of the vectors (or both if fixed) wouldn’t be functioning as intended. It would just drop to the floor.

Well it needs altering because the current loop will result in it never stopping at all.

You would want to check the position and check to see if its gone past or reached the end goal.

I know it did, because the line of code after the repeat loop ran, normally if it wasn’t functioning as intended, the line of code after the repeat loop wouldn’t run and the box would continue until I stopped testing.

This is false. CFrame.LookVector returns a unit vector (it has a length of 1), and the position of the CFrame doesn’t matter, only the orientation.

As for the loop, it is essentially pointless, as you are just setting the velocity then immediately checking if the velocity is what you just set it to. Therefore, the loop will run only once, so there is no need for it.

Now, as for how to solve it, I have two ideas. The first one is just to rotate the CFrame then take the LookVector of it. It would look something like this. This one will work, and if it doesn’t then I probably messed it up. You can also change the amount you rotate it to change the angle.

nearestObject.PrimaryPart.Velocity = (player.Character.Head.CFrame * CFrame.fromOrientation(math.rad(45), 0, 0)).LookVector * 50

The second one is to take an average of the two vectors, then take the unit of that. This one is more of what you asked for with merging two vectors, though I would recommend the one above. I don’t know if this will work, so if it doesn’t then try the one above. You could maybe do some sort of weighted average to change the angle.

local average = (player.Character.Head.CFrame.LookVector + player.Character.Head.CFrame.UpVector) / 2
nearestObject.PrimaryPart.Velocity = average.Unit * 50

I haven’t tested either, so if they go wrong let me know, the top one should work though.
For both of these, change the 50 to change the force it gets moved at, I just saw you used 50 in the original code so I used that as well.

This is strange. It didn’t function as intended. I didn’t want the rotation of the object, just the Y value (UpVector) and the X and Z value (lookVector) to somehow merge in some sort of “way”.

This can be achieved simply by adding the lookvector and the upvector then multiplying it by the amount of velocity you want to apply to the part then setting that to your velocity

So it would be something like this

part.Velocity = (HRP.CFrame.LookVector + HRP.CFrame.UpVector) * 50

give the result of

2 Likes