• Entity model drawing help (making it fly up and down)
    7 replies, posted
Hello, Big problem here. I'm currently making an entity, and I'd like it to spin on itself. I could do this fine, But now I want to make it fly up and down smoothly above the ground a bit, how would I do this? Like this. [t]http://i.imgur.com/WzxS9Pb.png[/t] (I froze the entity in air for this to happen of course) Here's my draw function. [CODE] function ENT:Draw() self:SetModelScale(0.9,0) self:DrawModel(true) self:SetPos(self:GetPos()+Vector(0,0,-90)) if self.ShouldRotate then self:SetRenderAngles(Angle(0,SysTime()*self.Settings.RotateSpeed,0)) end self:DrawShadow(false) self:SetColor(self:GetColorFromValue()) end [/CODE] I tried SetRenderOrigin and SetPos, that would work I guess but I need to cache the position so it doesn't go up straight, and I don't know how to do that. Thanks in advance! Bye
Add a math.cos(CurTime() ) to z argument of the pos.
It acts weird. [CODE]self:SetPos(self:GetPos()+Vector(0,0,math.cos(CurTime())/2))[/CODE] I'll post a video of what that does. But in the meantime, it just makes the entity act weird, move in the ground, etc... [editline]15th January 2014[/editline] Nope I didn't get it.
For rotation, this works: [lua]// // Handles repetitive smooth rotational calculations into 1 line - Josh 'Acecool' Moser // function math.RotationalYaw( _speed ) local _yaw = ( RealTime( ) * ( _speed or 180 ) ) % 360; _yaw = math.NormalizeAngle( _yaw ); return _yaw; end[/lua] For up/down motion, use this: [lua]// // Handles repetitive sin wave code into 1 line - Josh 'Acecool' Moser // function math.sinwave( _speed, _size, _abs ) local _sin = math.sin( RealTime( ) * ( _speed or 1 ) ) * ( _size or 1 ); if ( _abs ) then _sin = math.abs( _sin ); end return _sin; end[/lua] I turned these into helper-functions because they're commonly used and instead of repeating 3 or so lines each time, it can be used as one. Example usage for how I do my chat-bubble: [lua] local _sin = math.sinwave( 3, 5 ); local _yaw = math.RotationalYaw( 180 ) render.ClientsideModel( "models/extras/info_speech.mdl", 0, Player:GetPos( ) + Vector( 0, 0, _pos + _sin ), Angle( 0, _yaw, 0 ), 0.5, _color ) [/lua] You won't have render.ClientsideModel; it's another helper function. But it's easy to see what happens where:: function render.ClientsideModel( model, skin, pos, ang, size, color ) The model is info_speech.mdl, the position is the player position + an offset to the head + _sin which is a value that will go +/- 1. the input into sin changes the max-height and speed. The 3 is speed so it's pretty smooth, and 5 is the height. So +/- 5 = range of 10. abs just locks it to positive values only which would limit the range to exactly what you type, 5. RotationalYaw only argument is how fast you want it to spin. At 180, it travels half of full 360 degree rotation every second. Both functions are based on RealTime( ) meaning it'll sync across all clients fine; for it to look fine just set the yaw. Edit: Also, since it's an entity, with physics, make sure you always set the draw to be a static number above the ground as the base. This would take over the Player:GetPos( ) value in the render part on mine. Otherwise it'll look like a fish out of water; alternatively you could disable physics.
Thanks a lot Acecool but, The rotation works fine. But this in ENT:Draw() is just not reliable, either goes in floor or bugs out by any way. [CODE]self:SetRenderOrigin(self:GetPos()+Vector(0,0,math.sinwave(3,5)))[/CODE]
[QUOTE=Tenrys;43550979]Thanks a lot Acecool but, The rotation works fine. But this in ENT:Draw() is just not reliable, either goes in floor or bugs out by any way. [CODE]self:SetRenderOrigin(self:GetPos()+Vector(0,0,math.sinwave(3,5)))[/CODE][/QUOTE] That's because physics or something may be enabled on it. Also, sin waves go +/- 1; so with it going -5 at the lowest point, it's trying to go under-ground because GetPos points to the bottom of the entity instead of center/top. As said in my previous post; you'll need to do a trace, to get the ground level below the entity. Then set a fixed number above ground which you add to the Z. Replace self:SetRenderOrigin(self:GetPos()+Vector(0,0,math.sinwave(3,5))) with something like [lua]local _pos = self:GetPos( ); self:SetRenderOrigin(Vector(_pos.x,_pos.y,_dedicated_ground_z + _dedicated_height_above_ground + math.sinwave(3,5))) Solve for the _dedicated values. Or, disable physics / gravity on the entity so it won't fall, spawn it, drop to ground, then add the value where it should be. [/lua] Another thing you can try, if it's only going in the ground, is to enable abs on the sinwave which will ensure it won't go below the value. Then you'll have to increase the 5 to get the same distance of travel. use math.sinwave( 3, 10, true ); for the same distance of travel, but never using the negative value.
[lua] function ENT:PhysicsSimulate( physobj, delta ) physobj:SetVelocity( Vector(0,0,0) ) self:SetAngles( Angle(self:GetAngles().p,self:GetAngles().y,0) ) local pos = self:GetPos() local tr = util.TraceLine( { start = pos, endpos = pos - Vector(0,0,10), mask = MASK_SOLID_BRUSHONLY } ) return Vector(0,0,10000), Vector(0,0,tr.Hit and 0 or -1000), SIM_GLOBAL_ACCELERATION end [/lua]
I don't know anymore. I've never worked with traces, and Nookyava just pastes code without even... argh. What does Nookyava's code even mean? :/ I also don't know how to disable physics.
Sorry, you need to Log In to post a reply to this thread.