I have a swep which shows a preview(ghost) of the prop it is about to spawn, similar to dynamite and thruster. What I want to do is to be able to rotate the prop in this preview mode prior to actually spawning it. that way I don’t even have to use the physgun to rotate it and place it. I have gotten it to successfully rotate with +use but now I want it to rotate in 45 degree increments like with physguns shift + e. I can easily change the props yaw but I can’t change it’s roll properly.
I’ve been using the function RotateAroundAxis( Vector vAxis, Float fDegrees ). I can get the degrees fine but it is the vector I can’t get correct. For the yaw rotation I can just use a constant vector of (0, 0, 1) but for the roll it is a little more difficult as if I (player) rotates then the that vector must change. So I’ve tried using the players aimvector but that isn’t working correctly.
ALSO the main problem with the 45 degree increments is getting it to actually rotate in 45 degree increments. Because what if the ghost is already rotated a couple degrees, then it must rotate to the nearest 45 but I can’t figure that out. So far here’s my code for the rotation
if self.Owner:KeyDown( IN_USE ) then self.Freeze = true local cmd = self:GetOwner():GetCurrentCommand() local Xdegrees = cmd:GetMouseX() * 0.05 local Ydegrees = cmd:GetMouseY() * 0.05 local vAngle = self.Owner:GetCursorAimVector() if !self.Owner:KeyDown( IN_SPEED ) then self.Angle:RotateAroundAxis( vAngle:Normalize():Angle():Right(), Ydegrees ) self.Angle:RotateAroundAxis( Vector( 0, 0, 1 ), Xdegrees ) // Rotate p, y, and r in increments of 45 when shift + e is held down. else print ( self.Angle ) if ((self.Angle.p % 45) != 0 || (self.Angle.y % 45) != 0 || (self.Angle.r % 45) != 0 ) then self.Angle = Angle(0, 0, 0)--print ( "test" ) end --//Does the 45 and -45 degree rotations along the world's z-axis totalX = ( totalX + Xdegrees ) if totalX > 45 then totalX = 0 self.Angle:RotateAroundAxis( Vector( 0, 0, 1 ) , 45 ) elseif totalX < -45 then totalX = 0 self.Angle:RotateAroundAxis( Vector( 0, 0, 1 ) , -45 ) end --//Does the 45 and -45 degree rotations along the prop's totalY = ( totalY + Ydegrees ) if totalY > 45 then totalY = 0 self.Angle:RotateAroundAxis( vAngle:Normalize():Angle():Right(), 45 ) elseif totalY < -45 then totalY = 0 self.Angle:RotateAroundAxis( vAngle:Normalize():Angle():Right(), -45 ) end end else self.Freeze = false end
Got it working. I ended up keeping pretty much what I have and just rounding the numbers to the nearest 45 when shift was held down. It isn’t the best way as I should just be changing the angles to the nearest 45 only once when the prop might be messed up from just holding down e, then I should be rotating along the correct axis. but this way works fine.