AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Keeping entity at a distance (https://forums.alliedmods.net/showthread.php?t=334025)

Shadows Adi 08-23-2021 13:58

Keeping entity at a distance
 
Hello,
I am trying to keep a solid entity at a certain distance from player's view.
For example: At 150 units in front of player and tracking the distance from player to entity.

So far I am setting the entity velocity to keep it at a distance from player's view, but it works only if I move backwards, not forwards too.

entity_set_aim() function is called on every think of entity.

Code:
Code:
stock entity_set_aim(ent, player) {     static Float:origin[3], Float:ent_origin[3], Float:angles[3]     pev(player, pev_origin, origin)     pev(ent, pev_origin, ent_origin)           xs_vec_sub(origin, ent_origin, origin)     xs_vec_normalize(origin, origin)     vector_to_angle(origin, angles)           angles[0] = 0.0     if(entity_range(ent, player) > ENT_DISTANCE)     {         set_velocity(ent, angles)         angles[1] -= 90.0         set_pev(ent, pev_angles, angles)     } } stock set_velocity(ent, Float:angles[3]) {     static Float: Direction[3]     angle_vector(angles, ANGLEVECTOR_FORWARD, Direction)     xs_vec_mul_scalar(Direction, float(Speed), Direction) // float(-Speed)     set_pev(ent, pev_velocity, Direction) }

Edit: Just achieved to move entity forwards and backwards by making the Speed positive and negative. But the entity is not staying in front of player.

Natsheh 08-23-2021 14:40

Re: Keeping entity at a distance
 
https://forums.alliedmods.net/showpo...39&postcount=3

Can you attach some screenshots so we can imagine whats happening?

Shadows Adi 08-23-2021 15:07

Re: Keeping entity at a distance
 
Here is a short video: https://vimeo.com/591185759

I wanna update the entity's origin within x units from player's front angle in aiming direction.

Natsheh 08-23-2021 15:18

Re: Keeping entity at a distance
 
Quote:

Originally Posted by Shadows Adi (Post 2755993)
Here is a short video: https://vimeo.com/591185759

I wanna update the entity's origin within x units from player's front angle in aiming direction.

You can use this function shift_entity_position from the link I've posted in the previous post..

Shadows Adi 08-23-2021 16:50

Re: Keeping entity at a distance
 
It doesn't work, it keeps spinning the entity in a sphere.

Edit: forgot to get view offset. It does work now, thank you!

Shadows Adi 08-23-2021 18:07

Re: Keeping entity at a distance
 
When using this function, the entity doesn't make solid or touchable while it is in a wall.

Natsheh 08-24-2021 01:00

Re: Keeping entity at a distance
 
Quote:

Originally Posted by Shadows Adi (Post 2756011)
When using this function, the entity doesn't make solid or touchable while it is in a wall.

What do you mean whilst in the wall, explain?

Are you setting the entity maxs and mins?

Shadows Adi 08-24-2021 16:31

Re: Keeping entity at a distance
 
Quote:

Originally Posted by Natsheh (Post 2756033)
Are you setting the entity maxs and mins?

Yes

Quote:

Originally Posted by Natsheh (Post 2756033)
What do you mean whilst in the wall, explain?

I want to allow players to place the entity in walls and keep it's solidity.

Edit: I created the entity using info_target instead of func_wall and now seems to work right.
But, it's origin gets upwards if it touch a wall it seems. Video: https://vimeo.com/591807069

P.S.: I am dropping the entity every frame using EngFunc_DropToFloor right after using shift_entity_position()

Natsheh 08-25-2021 00:13

Re: Keeping entity at a distance
 
Show the code

Shadows Adi 08-25-2021 04:21

Re: Keeping entity at a distance
 
PHP Code:

RegisterHam(Ham_Think"info_target""ent_ThinkPre")

public 
ent_ThinkPre(iEnt)
{
    if(!
pev_valid(iEnt))
        return

    static 
className[32]
    
pev(iEntpev_classnameclassNamecharsmax(className))

    if(!
equali(classNameg_szClassName)) 
    {
        return
    }

    new 
id pev(iEntpev_owner)

    if(
is_user_alive(id))
    {
        new 
Float:fStartPos[3], Float:fvOFS[3], Float:fAngles[3]
        
pev(idpev_originfStartPos)
        
pev(idpev_view_ofsfvOFS)
        
pev(idpev_anglesfAngles)
        
fAngles[0] = 0.0
        
//fStartPos[2] += fvOFS[2]
        
set_pev(iEntpev_originfStartPos)
        
set_pev(iEntpev_anglesfAngles)
        new 
button get_user_button(id)
        if(
button IN_FORWARD)
        {
            
entity_set_aim(iEntidtrue);
        }
        else
        {
            
entity_set_aim(iEntid);
        }
    } 

    
shift_entity_position(iEnt100.00.030.0)
    
engfunc(EngFunc_DropToFlooriEnt)

    
set_pev(iEntpev_nextthinkget_gametime() + 0.01)
}

stock entity_set_aim(entplayerbool:bBackWards false

    static 
Float:origin[3], Float:ent_origin[3], Float:angles[3
    
pev(playerpev_originorigin)
    
pev(entpev_originent_origin
     
    
xs_vec_sub(originent_originorigin
    
xs_vec_normalize(originorigin
    
vector_to_angle(originangles
     
    
angles[0] = 0.0 
    
if(entity_range(entplayer) > ENT_DISTANCE)
    {
        
set_velocity(entanglesplayerbBackWards)
        
//angles[1] -= 90.0
        
set_pev(entpev_anglesangles)
    }

}

stock set_velocity(entFloat:angles[3], playerbool:bBackWards false)
{
    new 
FloatDirection[3
    
angle_vector(anglesANGLEVECTOR_FORWARDDirection
    
xs_vec_mul_scalar(DirectionbBackWards float(-Speed) : float(Speed), Direction)
    
set_pev(entpev_velocityDirection)
}

stock shift_entity_position(entFloat:forwFloat:rightFloat:up)
{
    new 
Float:vOrigin[3], Float:vAngle[3], Float:vForward[3], Float:vRight[3], Float:vEnd[3];
    
    
pev(entpev_originvOrigin);
    
pev(entpev_anglesvAngle);
    
    
angle_vector(vAngle,ANGLEVECTOR_FORWARDvForward); //or use EngFunc_AngleVectors
    
angle_vector(vAngle,ANGLEVECTOR_RIGHTvRight);
    
    
vEnd[0] = vOrigin[0] + (vForward[0] * forw) + (vRight[0] * right);
    
vEnd[1] = vOrigin[1] + (vForward[1] * forw) + (vRight[1] * right);
    
// Extract up value from vOrigin to make entitystay on ground when in walls but it seems that it didn't work too good
    
vEnd[2] = vOrigin[2] - up;

    
// debug only
    
client_print(0print_chat"origin2: %f"vOrigin[2])

    
// This don't help with anything, so I decided to comment it.
    /*static iTr2; iTr2 = create_tr2();
    engfunc(EngFunc_TraceLine, vOrigin, vEnd, DONT_IGNORE_MONSTERS, ent, iTr2);
    get_tr2(iTr2, TR_vecEndPos, vEnd);
    free_tr2(iTr2);*/

    
set_pev(entpev_originvEnd);




All times are GMT -4. The time now is 07:43.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.