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.

Raycasting

Discussion in 'Snippets' started by Vektor42O, Feb 15, 2017.

  1. Vektor42O

    Vektor42O Active Member

    Messages:
    77
    Likes Received:
    54
    Joined:
    Jan 24, 2017
    Hi!
    I hope this will work. This code works only on client side.
    http://gtan.codeshock.hu/index.php?page=raycasting
    Code:
    API.onUpdate.connect(function() {
    
        if(API.isControlPressed(25))//Is Player aiming
        {
            var aimPos = API.getPlayerAimingPoint(API.getLocalPlayer());     //getting aiming position in 3d world
            var camPos = API.getGameplayCamPos();                            //getting camera position in 3d world
    
            aimPos = new Vector3( 
                                    ((aimPos.X-camPos.X)*12)+camPos.X,
                                    ((aimPos.Y-camPos.Y)*12)+camPos.Y,
                                    ((aimPos.Z-camPos.Z)*12)+camPos.Z
                                );    //Set aimPos 12 times far away using camPos, because getPlayerAimingPoint does not always hit objects
    
            var rayCast = API.createRaycast(camPos, aimPos, 12, API.getLocalPlayer()); //create the raycast between camPos and aimPos, with detecting peds only, (ignoring yours just in case)
      
            if (rayCast.didHitEntity)//Is Raycast hits a (gta:n) entity
            {
                var hitPlayer = rayCast.hitEntity; //extract the player object
                API.sendChatMessage("You are aiming at " + API.getPlayerName(hitPlayer) + " right now!");
            }
        }
    });
    This is just an example. You should not use this code on 120Hz tickrate. It's ok in every 2 secs.

    [​IMG]
     
    Last edited: Feb 28, 2017
    Ariana and Lukeifer like this.

Share This Page