1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. GTA Network forum is now in archive mode.

Click on entity

Discussion in 'Snippets' started by Xenius, Jan 10, 2017.

  1. Xenius

    Xenius Member Donator

    Messages:
    48
    Likes Received:
    22
    Joined:
    Sep 26, 2016
    Hello.
    This script detect if you click on entity.

    getClickedEntity function by Hunfiu12 (https://forum.gtanet.work/index.php?threads/get-entity-by-cursor-click.814)
    Code:
    function getLocalPlayer(){
        return API.getLocalPlayer();
    }
    
    function getEntityData(ent, data){
        if (API.hasEntitySyncedData(ent, data))
            return API.getEntitySyncedData(ent, data);
        else
            return false;
    }
    
    API.onKeyDown.connect(function (sender, key) {
        if (key.KeyCode === Keys.M) {
            // if (!getEntityData(getLocalPlayer(), "spawned")) return;
            API.showCursor(!API.isCursorShown());
        }
    });
    
    function getClickedEntity(cursorPos, sensitivity = 0.1) {
        var clickedEntity = null;
        var cursorCoords = API.screenToWorld(cursorPos);
        var vecLineXStart = new Vector3(cursorCoords.X+sensitivity, cursorCoords.Y, cursorCoords.Z);
        var vecLineXEnd = new Vector3(cursorCoords.X-sensitivity, cursorCoords.Y, cursorCoords.Z);
        var vecLineYStart = new Vector3(cursorCoords.X, cursorCoords.Y+sensitivity, cursorCoords.Z);
        var vecLineYEnd = new Vector3(cursorCoords.X, cursorCoords.Y-sensitivity, cursorCoords.Z);
        var vecLineZStart = new Vector3(cursorCoords.X, cursorCoords.Y, cursorCoords.Z+sensitivity);
        var vecLineZEnd = new Vector3(cursorCoords.X, cursorCoords.Y, cursorCoords.Z-sensitivity);
        var vecLineXYZStart = new Vector3(cursorCoords.X+sensitivity, cursorCoords.Y+sensitivity, cursorCoords.Z+sensitivity);
        var vecLineXYZEnd = new Vector3(cursorCoords.X-sensitivity, cursorCoords.Y-sensitivity, cursorCoords.Z-sensitivity);
        var vecLineXYStart = new Vector3(cursorCoords.X+sensitivity, cursorCoords.Y+sensitivity, cursorCoords.Z-sensitivity);
        var vecLineXYEnd = new Vector3(cursorCoords.X-sensitivity, cursorCoords.Y-sensitivity, cursorCoords.Z+sensitivity);
        var vecLineYZStart = new Vector3(cursorCoords.X-sensitivity, cursorCoords.Y+sensitivity, cursorCoords.Z+sensitivity);
        var vecLineYZEnd = new Vector3(cursorCoords.X+sensitivity, cursorCoords.Y-sensitivity, cursorCoords.Z-sensitivity);
        var vecLineXZStart = new Vector3(cursorCoords.X+sensitivity, cursorCoords.Y-sensitivity, cursorCoords.Z+sensitivity);
        var vecLineXZEnd = new Vector3(cursorCoords.X-sensitivity, cursorCoords.Y+sensitivity, cursorCoords.Z-sensitivity);
        [
            API.createRaycast(vecLineXStart, vecLineXEnd, -1, null),
            API.createRaycast(vecLineYStart, vecLineYEnd, -1, null),
            API.createRaycast(vecLineZStart, vecLineZEnd, -1, null),
            API.createRaycast(vecLineXYZStart, vecLineXYZEnd, -1, null),
            API.createRaycast(vecLineXYStart, vecLineXYEnd, -1, null),
            API.createRaycast(vecLineYZStart, vecLineYZEnd, -1, null),
            API.createRaycast(vecLineXZStart, vecLineXZEnd, -1, null)
        ].every(function(ray, index) {
            if(ray.didHitEntity) {
                clickedEntity = ray.hitEntity;
                return false;
            }
            return true;
        });
        return clickedEntity;
    }
    
    API.onUpdate.connect(function(){
        if (API.isCursorShown()) {
            if (API.isControlJustPressed(25)) {
                var cursorPos = API.getCursorPosition();
                onClientClick("right", getClickedEntity(cursorPos));
            } else if (API.isControlJustPressed(24)) {
                var cursorPos = API.getCursorPosition();
                onClientClick("left", getClickedEntity(cursorPos));
            }
        }
    });
    
    function showCarMenu(ent) {
        API.sendChatMessage("This is a car!");
    }
    
    function onClientClick(btn, ent){
        if (btn == "left") {
            // left click
        } else if (btn == "right") {
            if (ent && API.doesEntityExist(ent)){
                var entType = API.getEntityType(ent);
                if (entType == 1)
                    showCarMenu(ent);
            }
            // right click
        }
    }
     
  2. Thias_Nietli

    Thias_Nietli Member

    Messages:
    13
    Likes Received:
    1
    Joined:
    Jan 1, 2017
    Can i find a client player with click on entity? Like: I click on player and this return a player hash?
     
  3. Xenius

    Xenius Member Donator

    Messages:
    48
    Likes Received:
    22
    Joined:
    Sep 26, 2016
    Code:
    if (entType == 6)
    {
        //do something
    }
    
     
  4. Thias_Nietli

    Thias_Nietli Member

    Messages:
    13
    Likes Received:
    1
    Joined:
    Jan 1, 2017
    In this method whent entitytype == 6 all The code on the if is for player clicked?
     
  5. Xenius

    Xenius Member Donator

    Messages:
    48
    Likes Received:
    22
    Joined:
    Sep 26, 2016
    Code:
    function onClientClick(btn, ent){
        if (btn == "left") {
            if (ent && API.doesEntityExist(ent)){
                var entType = API.getEntityType(ent);
                if (entType == 1)
                    API.sendChatMessage("this is a vehicle (left click)");
                else if (entType == 6)
                    API.sendChatMessage("this is a player (left click)");
            }
            // left click
        } else if (btn == "right") {
            if (ent && API.doesEntityExist(ent)){
                var entType = API.getEntityType(ent);
                if (entType == 1)
                    API.sendChatMessage("this is a vehicle (right click)");
                else if (entType == 6)
                    API.sendChatMessage("this is a player (right click)");
            }
            // right click
        }
    }
     

Share This Page