Projectile rotating

I am trying to create a projectile but somehow it keeps rotating to the right when the velocity is set, does anyone know why this happens/how to prevent this?

Show us the code you are using to set the projectile’s velocity.

I have tried multiple ways of setting the velocity but none of them worked, currently i am using :

[lua]function ENT:PhysicsUpdate(phys)
phys:ApplyForceCenter(self:GetUp()*self.Velocity)
end[/lua]

self.Velocity is defined on spawn, i tested it with 3000, 1000 and 10, all 3 of them eventually gave the same result.

Probably because the model you are using has a defined limit on the speed in which it can rotate.

Maybe take a look at this. **[PhysObj.SetDamping

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=PhysObj.SetDamping)**

Edit:

Ahh, shit. I thought you meant rotating the projectile object like a grenade type thing. My bad.

I’m having that issue as well, but using SetVelocity on a non-physics object. Apparently, when it reaches a certain speed, it just tends to move to the right for some reason.

Here you’re applying force in PhysicsUpdate, that will apply the force every frame, so it will necessarily accelerate forever and accumulate the slightest rounding error, and therefore rotating it in a random direction.
Just apply the force when it is initialized, that should do it. Or if you really want it to accelerate, stop applying the force when it reaches a certain velocity, or something.

I tried setting the speed at ENT:Initialize() but i still get the same problem.

[editline]08:48PM[/editline]

The projectile still moves forward but still rotates to the right, i tried PhysObj:SetDampning(0,1000) but it seemed like it did nothing.

Can you move the force part back to ENT:Initialize() and then show me the full code? Also, before that, try using SetVelocity instead of ApplyForceCenter.

[lua]AddCSLuaFile(“cl_init.lua”)
AddCSLuaFile(“shared.lua”)
include(‘shared.lua’)

// Edit these variables on your own ent.
ENT.ExplodeEffect = “wds_projectile_base_explosion”
ENT.TrailEffect = “wds_projectile_base_trail”
ENT.Velocity = 1000
ENT.Radius = 10
ENT.Damage = 10
ENT.Model = “models/wds/bullet.mdl”

function ENT:Initialize()
self:SetModel(self.Model)
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
if phys:IsValid() then
phys:Wake()
phys:EnableGravity(false)
phys:EnableDrag(false)
phys:ApplyForceCenter(self:GetUp()*self.Velocity)
end
local ed = EffectData()
ed:SetEntity(self)
util.Effect(self.TrailEffect,ed)
end

AccessorFunc(ENT,“Velocity”,“Speed”,FORCE_NUMBER)
AccessorFunc(ENT,“Radius”,“Radius”,FORCE_NUMBER)
AccessorFunc(ENT,“Damage”,“Damage”,FORCE_NUMBER)

function ENT:Think()
end

function ENT:PhysicsCollide(data,physobj)
self:Explode(data)
if self and self:IsValid() then self:Remove() end
end

function ENT:Explode(data)
WDS.Explosion(data.HitPos,self.Radius,self.Damage,{self},self.WDSO,self)
local ed = EffectData()
ed:SetOrigin(data.HitPos)
ed:SetMagnitude(self.Radius)
util.Effect(self.ExplodeEffect,ed)
end[/lua]

This is really awkward, I see nothing wrong here. Did you try it with something that usually works, like a melon?

Not yet, i am going to try that now.

[editline]09:55PM[/editline]

I just tried models/Gibs/HGIBS.mdl and models/props_combine/breenglobe.mdl but they have the same effect, it still rotates.

[editline]09:58PM[/editline]

Also the problem with setting the speed at the Initialize() function is that it wont go fast enough.

So, noone knows why it rotates?

Well there is only one problem left, it’s the weapon that launches it. Can I have a look at it?

[lua]function ENT:Shoot()
if !self or !self:IsValid() then return end
local ent = ents.Create(“wds_projectile_plasma”)
ent:SetPos(self:LocalToWorld(self.ShootDirection*self.ShootOffset))
ent:SetAngles(self.ShootDirection:Angle())
ent.WDSO = self.WDSO or self
ent.WDSE = self
ent:SetDamage(self.Damage)
ent:SetRadius(self.ExplodeRadius)
ent:SetSpeed(500)
ent:Spawn()
ent:Activate()
if self.ShootSound then self:EmitSound(self.ShootSound) end
end[/lua]

[editline]09:20AM[/editline]

The variables :

[lua]ENT.ShootDirection = Vector(0,0,1)
ENT.ShootOffset = 10[/lua]

Well, not sure, but in this situation, it doesn’t just rotate, it goes in the totally wrong direction.
Let’s see, ent:SetAngles(self.ShootDirection:Angle()) will rotate your bullet so it faces upwards, because self.ShootDirection is a unit vector that faces upwards.

But in the code of your bullet, you actually tell it to apply force in the same direction as its Up vector. Remember that this Up vector goes up, but only in the bullet’s relative space. Since your bullet is already facing upwards, self:GetUp() is actually an horizontal vector, and therefore, it goes in some random horizontal direction instead of actually going up.

Try self:GetForward() instead. :smile:

Can i add you to steam? (Your steam profile doesn’t work), it’d be easier to talk over steam, then i can also send you the complete project.

Sure, go ahead. Steam name is _Kilburn, obviously.

[editline]10:54AM[/editline]

Fixed the Steam profile thingy, didn’t even check if it was working before.

Done.

When i made my fighter i had the same problem with the cannons… I fixed it, but i have no clue how i did it… And my old HDD is half fucked, so i cant get the code.

Problem fixed, was caused by the projectile spawning inside the weapon somewhat :saddowns: