Jump to content

Back Player To ColShape


matin

Recommended Posts

Hey guys , i create some  colShape using createColRectangle i want when this event run

onColShapeLeave

back player to colShape Position and i try this :

local x2,y2,z2 = getElementPosition ( hitElement )
setElementPosition(hitElement,x2 - 4 ,y2 - 4, z2)

its work but some times in specific positions  , It works the opposite and send player out of colShape 

Link to comment

Hello matin,

this looks like a mathematical problem. What you want to calculate is the line intersection between the player position and a 2D rectangle. But I don't know where exactly the player should be dragged back to. Do you want push the player toward the center of the rectangle? Or do you want to pick the closest distance to the rectangle?

Link to comment
8 minutes ago, matin said:

@The_GTA  i just want example back player to his 1 secend ago position i just want dont let him leave , but i dont want teleport him to Center of Shape just close area near him

 

This is not that hard. If the player was already inside of the rectangle then you can remember his last position inside it and just reset him back to that position, like...

local last_valid_player_pos = {};

local shape; -- the colshape.

setTimer(
  function()
    for m,n in ipairs(getElementsByType("player")) do
      if (isElementWithinColShape(n, shape)) then
        last_valid_player_pos[n] = { getElementPosition(n) };
      else
        local last_valid_pos = last_valid_player_pos[n];
        
        if (last_valid_pos) then
          setElementPosition(n, unpack(last_valid_pos));
        end
      end
    end
  end, 1000, 0
);

If you want to handle vehicles aswell then you have to extend the script. You also should handle the case if the script is started when the player was never inside the rectangle.

Edited by The_GTA
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...