• Custom ViewModel Sway
    28 replies, posted
A long time ago I made a post about SwayScale and its jittering. Now Since I know SwayScale causes some extreme Jitter when the Viewmodel is changed, I decided to create my own custom Sway. The Issue is most of my LUA experience is more gamemode related, not weapon bases. I did not know if anyone could at least point me in the right way. I know it involves GetViewModelPosition as I have a script (thanks to help from someone on steam) That uses Lerp To smoothly transition between Ironsighting, standard, and Running Positions. Here's My Code For that: [code] function SWEP:GetViewModelPosition( pos, ang ) bIron = self:GetIronsight() Running = self:IsRunning() if bIron then self.SwayScale = 0 self.BobScale = 0.05 else self.SwayScale = 0 self.BobScale = .5 end if bIron and !Running then if self.IronSightsPos and self.IronSightsAng then SetPos = self.IronSightsPos SetAng = self.IronSightsAng end VmSmooth = self.IronsightTime * 30 elseif Running and !bIron then if self.RunSightsPos and self.RunSightsAng then SetPos = self.RunSightsPos SetAng = self.RunSightsAng end VmSmooth = self.RunSightTime * 30 elseif !bIron and !Running then SetPos = Vector(0,0,0) SetAng = Vector(0,0,0) VmSmooth = 5 end WantedPos = LerpVector(FrameTime() * VmSmooth, WantedPos, SetPos) WantedAng = LerpVector(FrameTime() * VmSmooth, WantedAng, SetAng) VmPos.x = WantedPos.x VmPos.y = WantedPos.y VmPos.z = WantedPos.z VmAng.x = WantedAng.x VmAng.y = WantedAng.y VmAng.z = WantedAng.z Right = ang:Right() Up = ang:Up() Forward = ang:Forward() ang = ang * 1 ang:RotateAroundAxis( Right, VmAng.x ) ang:RotateAroundAxis( Up, VmAng.y ) ang:RotateAroundAxis( Forward, VmAng.zkil ) pos = pos + VmPos.x * Right pos = pos + VmPos.y * Forward pos = pos + VmPos.z * Up return pos, ang end [/code] I am not asking for someone to code this for me. I just need to know how to check for when the player turns and then somehow get how fast the player is turning and move the ViewModel's Position so much based on that turning speed. Thank you all for the help! If these calls I'm asking for help on are on the Garry's Mod 13 Wiki or the Mauritz.tv copy I apologize for wasting your time. I've looked and looked and cannot seem to find what I need.
One way I've done it was to create an angle local to the script and relate it back to the player's current EyeAngle. Depending on how you relate them to each other, it can tell you the delta of the player's eye angle which would translate to how fast the player has turned, and with that, you can move the ViewModel accordingly.
So you mean just create a static Angle like say [code] referenceangle = Angle(0, 0, 0) [/code] and compare that to the player's eyeangles in some shape or form. I apologize If I don't understand.
No, then that'd just be the player's EyeAngle and you can't really use that. Something like [code] local OldEyeAng = Angle(0,0,0) function SWEP:SomethingThatsRunEveryFrame(args) local EyeAngDelta = self.Owner:EyeAngles() - OldEyeAng //stuff OldEyeAng = self.Owner:EyeAngles() end [/code] Something along the lines of that. You could make it like, self.OldEyeAng if you really want.
OH okay, I thought what I thought you meant was weird. Honestly I feel dumb for not realizing what you meant now since you showed me a good example, my bad. Anyways thank you for getting me started, this should help me a lot. I had to look up what you meant by The Eye Angle Delta but I understand now. Thank you! Update: I just want to mention I was able to successfully make a weapon sway with your help. I want to give you a huge thanks for helping me with this.
[QUOTE=Stormtrooper595;43793885] Update: I just want to mention I was able to successfully make a weapon sway with your help. I want to give you a huge thanks for helping me with this.[/QUOTE]May I ask how you went about doing this? I'm interested as I hate the issues with the current sway movement.
I just used what awcmon showed me and then used a basic LerpAngle System with FrameTime() multiplied by a constant as the percentage. I then proceeded to use RotateAroundAxis to update the pitch, yaw, and roll values I recieved from that Lerp. This in turn made a smooth weapon sway. It's not the best in the world, but It works. If you're not sure what I mean, Just add me on steam and I'll help you out on there.
[QUOTE=Stormtrooper595;43819149]I just used what awcmon showed me and then used a basic LerpAngle System with FrameTime() multiplied by a constant as the percentage. I then proceeded to use RotateAroundAxis to update the pitch, yaw, and roll values I recieved from that Lerp. This in turn made a smooth weapon sway. It's not the best in the world, but It works. If you're not sure what I mean, Just add me on steam and I'll help you out on there.[/QUOTE]Thanks. I actually tried doing that myself with almost the same method but gave up after a bit haha. I'll give it another go later.
I haven't tested this yet, just gave it a crack. Am I on the right track? [lua] function SWEP:CustomSway( ang ) local EyeAngDelta = self.Owner:EyeAngles() - OldEyeAng OldEyeAng = self.Owner:EyeAngles() NewAng = LerpAngle( FrameTime() * 1, OldEyeAng, EyeAngDelta ) ang = ang * 1 ang:RotateAroundAxis( ang:Right(), NewAng.x ) ang:RotateAroundAxis( ang:Up(), NewAng.y ) ang:RotateAroundAxis( ang:Forward(), NewAng.z ) return ang end [/lua]
Yeah, you are on the right track. On the multiplicator for FrameTime(), you can increase and decrease that to your liking to make the sway more or less noticeable. Also on the RotateAroundAxis, x y and z are for vectors not angles, Use something like this: [code] ang:RotateAroundAxis( ang:Right(), NewAng.p) ang:RotateAroundAxis( ang:Up(), NewAng.y) ang:RotateAroundAxis( ang:Forward(), NewAng.r) [/code]
[QUOTE=Stormtrooper595;43832532] Also on the RotateAroundAxis, x y and z are for vectors not angles, Use something like this: [code] ang:RotateAroundAxis( ang:Right(), NewAng.p) ang:RotateAroundAxis( ang:Up(), NewAng.y) ang:RotateAroundAxis( ang:Forward(), NewAng.r) [/code][/QUOTE] You can do it with a vector just fine. It just takes a number argument, whether it's a vector or an angle makes no difference because it's not taking the userdata.
-snip- I misread you. I was telling him that he has to use p y r since he used an angle not a vector as the angle stores them in p y r not x y z. I thought you were saying that it doesnt matter if he uses x y z or p y r, silly me. and Yes, I know that you can use vectors in rotatearoundaxis, as I did in my code previously, I just was not aware that was what you were talking about, It's 6 AM where I'm at.
[QUOTE=Stormtrooper595;43832532]Yeah, you are on the right track. On the multiplicator for FrameTime(), you can increase and decrease that to your liking to make the sway more or less noticeable. Also on the RotateAroundAxis, x y and z are for vectors not angles, Use something like this: [code] ang:RotateAroundAxis( ang:Right(), NewAng.p) ang:RotateAroundAxis( ang:Up(), NewAng.y) ang:RotateAroundAxis( ang:Forward(), NewAng.r) [/code][/QUOTE]Alright, thanks man. Also, to actually implement it would I just need to first remove the default sway by setting the sway scale to 0 and then simply run the new custom function in the SWEP think function with self:CustomSway()? One more thing, awcmon's example, what would be a good idea to have the args as? Perhaps ang for angle?
[QUOTE=Stormtrooper595;43833185]Ah Okay, did not know that, thank you![/QUOTE] You did that in the code you posted :|
[QUOTE=OzymandiasJ;43833232]You did that in the code you posted :|[/QUOTE]He did say he got it from someone on Steam. [editline]8th February 2014[/editline] On a side note, I use my own similar sort of GetViewModelPosition for my weapon base which uses LerpVector for positions and I happen to get extreme jittering of the weapon between movements. It's unbearable haha. I'll work on trying to fix the issue tomorrow using a different method.
Extreme Jittering? That's weird, you have the standard WeaponSway set to 0 right? Also, I run my function through GetViewModelPosition. You might be able to run it through the think function, but I just run mine through that. Now if only I could figure out how to get it to work with CalcView...so I can make the camera move based on a reload animation.
[QUOTE=Stormtrooper595;43833398]Extreme Jittering? That's weird, you have the standard WeaponSway set to 0 right? Also, I run my function through GetViewModelPosition. You might be able to run it through the think function, but I just run mine through that. Now if only I could figure out how to get it to work with CalcView...so I can make the camera move based on a reload animation.[/QUOTE]The jittering isn't with the sway, it's just with the ironsights and running positions but I'll probably be able to figure it out tomorrow. Also, I think you can actually do reload movement in GetViewModelPosition although I haven't done it before so not 100% sure.
The way I did it was make the Sway edit the Angles AFTER the ironsight and running positions all in the same function. Works fine for me. That customSway function might be screwing with GetViewModelPositions if that is where your ironsights and running position codes are at since the CustomSway is returning an angle as well as the GetViewModelPositions. In my case, it adds in the weapon sway right before the angle is returned.
[QUOTE=Stormtrooper595;43833484]The way I did it was make the Sway edit the Angles AFTER the ironsight and running positions all in the same function. Works fine for me. That customSway function might be screwing with GetViewModelPositions if that is where your ironsights and running position codes are at since the CustomSway is returning an angle as well as the GetViewModelPositions. In my case, it adds in the weapon sway right before the angle is returned.[/QUOTE]I got off my computer a while ago for the night so I'll do it tomorrow and let you know how I go. Thanks for your help by the way.
No problem! Hopefully it works out for you! :)
Okay I'm back to trying to code this... haha. What am I doing wrong? This doesn't seem to be working and there are no errors: [lua] local OldEyeAng = Angle( 0, 0, 0 ) function SWEP:CustomSwayMovement() local EyeAngDelta = self.Owner:EyeAngles() - OldEyeAng OldEyeAng = self.Owner:EyeAngles() NewAngle = LerpAngle( RealFrameTime() * 5, OldEyeAng, EyeAngDelta ) ang = ang * 1 ang:RotateAroundAxis( ang:Right(), NewAngle.p ) ang:RotateAroundAxis( ang:Up(), NewAngle.y ) ang:RotateAroundAxis( ang:Forward(), NewAngle.r ) return ang end [/lua]
As far as I can see it looks correct. You have this code hooked to something right? That returned angle has to go somewhere.
[QUOTE=Stormtrooper595;43847324]As far as I can see it looks correct. You have this code hooked to something right? That returned angle has to go somewhere.[/QUOTE] That causes errors for me too as well.
[QUOTE=Stormtrooper595;43847324]As far as I can see it looks correct. You have this code hooked to something right? That returned angle has to go somewhere.[/QUOTE]Sorry to ask, this is really confusing me haha but how exactly would I apply this into SWEP:GetViewModelPosition? Would I just put self:CustomSwayMovement() into the function or do I need to do something else as well?
The easiest way is something like this: [code] local OldEyeAng = Angle( 0, 0, 0 ) function SWEP:GetViewModelPosition( pos, ang ) local EyeAngDelta = self.Owner:EyeAngles() - OldEyeAng OldEyeAng = self.Owner:EyeAngles() NewAngle = LerpAngle( RealFrameTime() * 5, OldEyeAng, EyeAngDelta ) ang = ang * 1 ang:RotateAroundAxis( ang:Right(), NewAngle.p ) ang:RotateAroundAxis( ang:Up(), NewAngle.y ) ang:RotateAroundAxis( ang:Forward(), NewAngle.r ) return pos, ang end [/code] I just took your code and directly put it into the GetViewModelPosition function. It's much easier than hooking it, unless you're doing a lot with GetViewModelPosition.
[QUOTE=Stormtrooper595;43865272]The easiest way is something like this: [code] local OldEyeAng = Angle( 0, 0, 0 ) function SWEP:GetViewModelPosition( pos, ang ) local EyeAngDelta = self.Owner:EyeAngles() - OldEyeAng OldEyeAng = self.Owner:EyeAngles() NewAngle = LerpAngle( RealFrameTime() * 5, OldEyeAng, EyeAngDelta ) ang = ang * 1 ang:RotateAroundAxis( ang:Right(), NewAngle.p ) ang:RotateAroundAxis( ang:Up(), NewAngle.y ) ang:RotateAroundAxis( ang:Forward(), NewAngle.r ) return pos, ang end [/code] I just took your code and directly put it into the GetViewModelPosition function. It's much easier than hooking it, unless you're doing a lot with GetViewModelPosition.[/QUOTE]Thanks, although I've actually got a lot of stuff in GetViewModelPosition so I'd rather have a seperate function for the sway movement and just hook that into GetViewModelPosition.
I was able to get a result, although the result was absolutely terrible and the view movement was utterly crazy. I put this in my GetViewModelPosition: [lua] local SwayAng = self:CustomSwayMovement() if ( self.SwayScale == 0 ) then ang = ang + SwayAng end [/lua]
[QUOTE=ShadowRanger;43867319]I was able to get a result, although the result was absolutely terrible and the view movement was utterly crazy. I put this in my GetViewModelPosition: [lua] local SwayAng = self:CustomSwayMovement() if ( self.SwayScale == 0 ) then ang = ang + SwayAng end [/lua][/QUOTE] That's because CustomSwayMovement is supposed to be replacing the ang, not being added to it.
[QUOTE=OzymandiasJ;43868215]That's because CustomSwayMovement is supposed to be replacing the ang, not being added to it.[/QUOTE]I've tried removing the + and a few other different ways but they make no positive difference. Is there anything wrong with my actual sway movement function? Have I set the right angles to use and stuff?
Sorry, you need to Log In to post a reply to this thread.