# how to apply velocity angle to an entity

hi everybody ! i’m creating a bazooka swep but something goes wrong
i don’t know how to explain my problem, but i made this

http://img4.hostingpics.net/pics/696486problemvelocity.jpg

the red lines is the “eye angle” of the entity and the black line is this velocity

this is the code :

weapon :

``````

function SWEP:PrimaryAttack()

if SERVER then
local ent = ents.Create("bazooka_bullet")

ent:SetPos(self.Owner:GetShootPos())
ent:Spawn()

local phys = ent:GetPhysicsObject()

local looking = self.Owner:EyeAngles()
looking.p = angleToShoot + 90
local datVector = looking:Up()

phys:SetVelocity(datVector * 9)
ent:SetAngles( ent:GetVelocity():Angle() )

end

end

``````

entity :

``````

function ENT:Think()

self:SetEyeTarget( self:GetVelocity()  ) --nothing

end

``````

these following lines i’ve tried make a weird bug: it put the bullet at -90° and stop it

``````

self:SetAngles( self:GetVelocity():Angle() )
self:SetAngles( self:GetForward():Angle() )

self:SetAngles( self:AlignAngles( self:GetForward():Angle(), self:GetAngles() ) )

phys:SetInertia( self:GetForward() ) -- also tried with GetVelocity() ...

etcccc....

``````

so i’ve tried everything … but nothing works

someone can help me ?

thanks

Try setting the angle to the angular translation of the velocity: http://wiki.garrysmod.com/page/Vector/Angle

This line:
self:SetAngles( self:GetVelocity():Angle() )
which you posted, should work if you put it in Think instead of SetEyeTarget.

SetEyeTarget is for models with eyes, like human ragdolls and NPCs. It shouldn’t do anything on a model with no eyes.

it should work but he doesn’t

like i said, this make entity bug, it stop it and make him at 90°

like this : [ self:SetAngles( self:GetVelocity():Angle() ) ]
before first entity think

http://img15.hostingpics.net/pics/140441tg1.jpg

after first time entity think

http://img15.hostingpics.net/pics/623406tg2.jpg

i have try SetEyeTarget() because its the last thing i tried …

Is the entity actually moving? If not, its velocity would be (0, 0, 0) and the angle would then be the same.

the entity move 0.5 sec and after it stop …

when i remove “self:setangle…” it move as like the first post image ,like this :

``````
print(self:GetAngles() , self:GetVelocity():Angle() )

``````
``````

-45.000 135.384 0.000	    316.228 135.384 0.000
-45.000 135.384 0.000	    318.660 135.384 0.000
-45.000 135.384 0.000	    321.301 135.384 0.000
-45.000 135.384 0.000	    324.169 135.384 0.000
-45.000 135.384 0.000	    327.280 135.384 0.000
-45.000 135.384 0.000	    330.646 135.384 0.000
-45.000 135.384 0.000	    334.276 135.384 0.000
-45.000 135.384 0.000	    338.172 135.384 0.000
-45.000 135.384 0.000	    342.325 135.384 0.000
-45.000 135.384 0.000	    346.717 135.384 0.000
-45.000 135.384 0.000	    351.315 135.384 0.000
-45.000 135.384 0.000	    356.073 135.384 0.000
-....
....

``````

So what happens when you set the angle to the GetVelocity:Angle? It seems to be outputting correctly.

``````

function ENT:Think()

print("before : ",self:GetAngles(),self:GetVelocity():Angle())
self:SetAngles( self:GetVelocity():Angle() )
print("after : ",self:GetAngles(),self:GetVelocity():Angle())

end

``````

cosole output :

``````

before : 	-45.000 133.556 0.000	316.308 133.556 0.000
after : 	316.308 133.556 0.000	90.000 0.000 0.000
before : 	-43.692 133.556 0.000	90.000 0.000 0.000
after : 	90.000 0.000 0.000	90.000 0.000 0.000
before : 	90.000 180.000 180.000	90.000 0.000 0.000
after : 	90.000 0.000 0.000	90.000 0.000 0.000
before : 	90.000 180.000 180.000	90.000 0.000 0.000
after : 	90.000 0.000 0.000	90.000 0.000 0.000
before : 	90.000 180.000 180.000	90.000 0.000 0.000
after : 	90.000 0.000 0.000	90.000 0.000 0.000
before : 	90.000 180.000 180.000	90.000 0.000 0.000
after : 	90.000 0.000 0.000	90.000 0.000 0.000

....

``````

i don’t understand what happens …

if you find the original code of the HL2 rocket launcher, this can help !

Does the entity stop moving when this happens? Maybe setting the angle resets the velocity

[editline]26th September 2016[/editline]

you could use

Entity:SetLocalAngularVelocity to adjust the angle instead, that way you’re working with the physics engine rather than against it, but you’d have to do the math to choose the angular velocity to set

[editline]26th September 2016[/editline]

Or possibly use

Entity:GetAbsVelocity instead of GetVelocity in the snippet you posted above?

if setting the angle for some reason is freezing it, you really can just store the velocity in a variable, set the angle to the variable’s angle, then set the velocity to the variable

i feel like i had to deal with something like that for a harpoon once

YES there it is :

``````

function ENT:PhysicsUpdate(phys)

local angles = phys:GetAngles()
local velocity = phys:GetVelocity()

phys:SetAngles( Angle(angles.p + 1 ,angles.y ,angles.r)     )
phys:SetVelocity(velocity)

end

``````

the entity move ! but now need to find witch angle to apply ?

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOHhhhhhhh

well done :

``````

function ENT:PhysicsUpdate(phys)

local angles = phys:GetAngles()
local velocity = phys:GetVelocity()

--print(angles, angles:Forward() , velocity , velocity:Angle() )

phys:SetAngles( velocity:Angle() )
phys:SetVelocity(velocity)

end

``````

we have all right from the beginning !
just forget to apply a new velocity …

finally …
thanks !