How would I make a position vector3 increase the closer someone gets to a part?

I am trying to make it where a given vector3 value increases the closer the player is to a part, I’ll then use that value for some velocity constraints but that doesn’t matter here.

I took code from another thread but the thread I took the code from uses Min and Max and I don’t want to use that, I just want the position to decrease when they are closer. Here is the link

Right now I’m just multiplying the value from that code, but it doesn’t work because at the end of the day the position still increases when they are farther.

math.clamp(MIN + (MAX - MIN)*(1-((char.PrimaryPart.Position - tchar.PrimaryPart.Position).Magnitude/maxdistance)), MIN, MAX)

2 Likes

Just calculate the distance between the 2 points:

(char.PrimaryPart.Position - tchar.PrimaryPart.Position).Magnitude
-- returns a number, which indicates the distance in studs
2 Likes

Magnitude decreases the closer they get to the other person I’m trying to figure out how to make it decrease
And I want a vector3 value not a number

1 Like

Not too sure if this would work, but you could try comparing each element of the Vector3:

local part = --your part here
local hrp = --the HumanoidRootPart of the player's character here

--then, when checking:
local partPos = part.CFrame.Position
local playerPos = hrp.CFrame.Position

local distance = Vector3.new(
    partPos.X - playerPos.X,
    partPos.Y - playerPos.Y,
    partPos.Z - playerPos.Z
)

distance = -(distance)

magntiude doesn’t increase the closer two things are, as calculated by @Vlado_RBLX
in his code, magnitude decreases as the distance between char and tchar decreases

2 Likes

Sorry I have been saying it wrong this whole time, I want the vector3 to INCREASE when they are closer not decrease

i’m confused as what you mean by ‘increase’? a vector3 is composed of a direction and a magnitude, not one value(duh)
i’m assuming you want the magnitude to increase as you get closer, and the direction to be at the other character(correct me if i’m wrong)
to do this, you need to define exactly how big you want it to get as the distance between characters changes,
to do this, you could use a linear falloff with a maximum value, minimum value, and maximum distance, or some other function like 1/x where x is the distance
then, once you have your function, set the vector (between the players) magnitude to the new magnitude like this

local maxDist = 100
local minValue = 0
local maxValue = 100
local offset = (char.PrimaryPart.Position - tchar.PrimaryPart.Position)--offset between players
local mag = offset.Magnitude
local newMagnitude
if mag<=maxDist then
    newMagnitude = ((maxDist-mag)/maxDist)*maxValue
else
    newMagnitude = minValue
end
local vector = newMagnitude*offset.Unit--unit is fixed magnitude of 1

not sure if that exact code will work but you get the idea

1 Like

Continue from there will be local playerCFrame = player.Character.CFrame + Vector(your change)

The player varaible is the game.players.local player one.

I have a question about this so this is only increasing the magnitude? I’m using a vector3 as a velocity and I want the velocity to be based on the players position from the other players position, the problem is that I want the velocity to be higher when they are closer and lower when they are farther.

yes, this is only increasing the magnitude
you can use something else as the unit, but i just assumed you wanted to have the vector pointing to the other player

i want to increase vector3 value the closer they are thats all i need

1 Like

i understand that, but it still matters what direction you want the vector to point in. if you are making a sort of dash towards the other player then ‘towards the other player’ is your direction

i want the vector to go opposite from the player like move away from them, i want them to get pushed back more when they are closer and less when they are farther

you can use your own distance function, or, for the code I previously posted, just replace

local vector = newMagnitude*offset.Unit

with

local vector = newMagnitude*-offset.Unit

which makes the vector point away(negating a vector gets the vector in the opposite direction)
(you could also do this by switching the order when calculating offset)

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