Make object "see" player/spawning rpgs

The model for the entity I’m building requires me to set Pose Parameters to make it swivel it’s head to look at players. I already have code to find the nearest player, and can successfully edit Pose Parameters, and even make the object point at the player, to an extant. Often, it’s aim will seem mostly spot on, but sometimes, it will aim rather far off, to the point where it doesn’t even seem to be facing the player any more, thus ruining the point.

The way I set it up quite frankly seems a little backwards, but I can’t really seem to find a better solution. Basically, I spawn a “reference object” at the initialization stage:


	self.Entity:SetAimReference(ents.Create("prop_dynamic"))
	--self.Entity:GetAimReference():SetModel("models/vehicles/vehicle_blackout_e1_dogintro.mdl") --this model is invisible, so players won't see it when I'm done
	self.Entity:GetAimReference():SetModel("models/props/radio_reference.mdl") --this is so I can see it for debugging purposes.
	self.Entity:GetAimReference():SetPos(self.Entity:GetPos())-- + self.Entity:GetUp()* 90 + self.Entity:GetRight() * -20) --this was used to position it earlier, the "SetParentAttachment" line overrides this...
	self.Entity:GetAimReference():SetAngles(self.Entity:GetAngles())
	self.Entity:GetAimReference():SetParent(self.Entity)
	self.Entity:GetAimReference():Fire("SetParentAttachment",*a functional attachment point on the model, near it's head*,0)
	self.Entity:GetAimReference():Spawn()
	
	print("lel"..self.Entity:GetAimReference():GetParent():GetName())

Then I force that object to always face the player, and use the reference objects angles to set the aim_pitch and aim_yaw pose parameters, both of which go from -180 to 180. I have some added offsets to make them work better with the model:


--Somewhere in ENT:Think()--
		self.Entity:GetAimReference():PointAtEntity(nearestPlayer) --reference object MUST face the player!
	
--This section sets up the pitch value
		local finalPitch = self.Entity:GetAimReference():GetAngles().p - 190

		finalPitch = finalPitch % 360

		if(finalPitch < 0) then
			finalPitch = finalPitch + 360
		end

		finalPitch = finalPitch - 180
	
		--print("finalpitch:"..finalPitch.."lel")

--this section sets up the yaw value
		local finalYaw = self.Entity:GetAimReference():GetAngles().y - 90
	
		finalYaw = finalYaw % 360

		if(finalYaw < 0) then
			finalYaw = finalYaw + 360
		end

		finalYaw = finalYaw - 180

		self:SetPoseParameter( "aim_pitch", finalPitch)
		--print("pitch is "..finalPitch)
		self:SetPoseParameter( "aim_yaw", -finalYaw)
		--print("yaw is "..-finalYaw)

Due to the way the head piece of the model swivels around, I don’t think this will work for the longterm, as there’s no decent center point to place the model to ensure that the heads’ aim is always true. Any thoughts on other methods to get this done would be very much appreciated.

As for the second issue, I can’t get it to even fire! I tried to rework the code from this topic to be spawned via an entity, but the rocket doesn’t even seem to move, just explode on the spot. It’s hard to say for sure, but judging by the fact that it leaves explosion marks on the floor, maybe the rockets are just falling in place :I Furthermore, the explosions don’t cause any damage, even when your inside them as they explode. The reworked code is below (keep in mind that I haven’t quite done all the things I’d like to do with it, for example the rocket spawn position doesn’t rotate relative to the head):


local r = ents.Create("rpg_missile")
   		r:SetPos(self.Entity:GetPos() + self.Entity:GetForward() * 72 ) --this works; it is placed roughly in the same direction away from the object
  		r:SetAngles( self.Entity:GetAimReference():GetAngles() ) --this uses the reference model angles, as that's just simpler here.
  		r:SetOwner(self.Entity) --object I'm using here is non-solid at the moment, so this is currently irrelevant...
  		r:Spawn()
  		r:SetVelocity( r:GetForward() * 9001 ) --I'm not sure if this is actually doing it's job or not... 

I apologize for the wall of text and code, but I’ve been fiddling with this for several straight hours, and fixed up a lot of things, and gotten stuck with this. Google hasn’t been much help, either…

do you have any videos of the issue? im assuming GetAimReference and SetAimReference are custom functions, as i cant find them on the wiki.

Yes, that is simply a network variable I setup in ENT:SetupDataTables(). That variable is used to hold the reference object for later use.

Here’s the video showing it’s less then satisfactory aiming (skip to about 1:26 to get right to the object at hand):

I hope that clears things up.