Jump to content

Target is disappearing


Rick
 Share

Recommended Posts

I have a target that is disappearing and I can't figure out why.

 

Here is the code to my 3rd person object. The idea behind this is that you place this object where you want the camera to start. You make another object the target of the camera object. When you run the game it then gives mouse controls to rotate around the object you made the target? It would also move along with the target. That all works. The issues I'm having is more in the editor. I'm using 2 pivots. One at the base of the target object and one where the camera entity in the editor should be. This also works at first. When I run the game the first time it all works fine. When I exit the game it then removes the target link for some reason. This is problem number 1. Problem number 2 is after I exit the game mode, the cube I have for debugging reasons for some reason moves slightly away from the camera object. I can't figure out why. Any help would be great. Thanks!

 

require("scripts/class")
require("scripts/loop")

--- create the class
local class=CreateClass(...)

function class:InitDialog(grid)
self.super:InitDialog(grid)

group=grid:AddGroup("Position")
group:AddProperty("PosOffset",PROPERTY_VEC3,"","Position Offset" )
end


function class:CreateObject(model)
local object=self.super:CreateObject(model)

object.model = model
object.msgOnce = false
object.initOnce = false
object.leftMouseDown = false
object.camRotationX = 0
object.camRotationY = 0
object.mouseX = 0
object.mouseY = 0
object.cameraPivot = CreatePivot()
object.playerPivot = CreatePivot()
object.cameraPivot:SetParent(object.playerPivot, 1)

object.PlayerPositionOffsetX = 0
object.PlayerPositionOffsetY = 0
object.PlayerPositionOffsetZ = 0

-- debugging code
object.cameraCube = CreateCube(object.cameraPivot)
object.cameraCube:SetScale(Vec3(.25, .25, .25))
object.playerCube = CreateCube(object.playerPivot)
object.playerCube:SetScale(Vec3(.25, .25, .25))

function object:Round(num, idp)
	local mult = 10^(idp or 0)
	return math.floor(num * mult + 0.5) / mult
end

function object:Free()
	object.cameraPivot:Free()
	object.playerPivot:Free()

	-- debugging code
	object.cameraCube:Free()
	object.playerCube:Free()
end

function object:SetKey(key, value)
	--if key == "PosOffset" then
	--	Notify("Hello")
	--	local offset=string.Explode(value,",")
	--	object.PlayerPositionOffsetX = tonumber(offset[1])
	--	object.PlayerPositionOffsetY = tonumber(offset[2])
	--	object.PlayerPositionOffsetZ = tonumber(offset[3])
	--end
end

function object:GetKey(key, value)
end

function object:Update()
	local t = object.model:GetTarget(0)

	if t ~= nil then
		if object.initOnce == false then
			object.initOnce = true
			--pos.x = pos.x + object.PlayerPositionOffsetX
			--pos.y = pos.y + object.PlayerPositionOffsetY
			--pos.z = pos.z + object.PlayerPositionOffsetZ

			--object.playerPivot:SetPosition(Vec3(pos.x, pos.y, pos.z))
			object.playerPivot:SetPosition(EntityPosition(t))
			object.cameraPivot:SetPosition(EntityPosition(object.model), 1)
		end
	end

	if GetGlobalString("mode") == "GAME_MODE" then
		if t == nil then
			if object.msgOnce == false then
				msgOnce = true
				Notify("You must assign a target for the 3rd person camera!")
			end
		else
			if MouseHit(1) == 1 then
				object.mouseX = MouseX()
				object.mouseY = MouseY()
				MoveMouse(GraphicsWidth() / 2, GraphicsHeight() / 2)

				HideMouse()
			end

			if MouseDown(1) == 1 then
				local mx
				local my
				mx = MouseX() - object:Round(GraphicsWidth()/2, 0)
				my = MouseY() - object:Round(GraphicsHeight()/2, 0)
				MoveMouse(GraphicsWidth()/2, GraphicsHeight()/2)

				object.camRotationY = object.camRotationY + mx / 4.0	-- this is left and right
				object.camRotationX = object.camRotationX - my / 4.0	-- this is up and down

				if object.camRotationX > 25 then
					object.camRotationX = 25
				end
				if object.camRotationX < -55 then
					object.camRotationX = -55
				end

				object.leftMouseDown = true
				object.playerPivot:SetRotation(Vec3(object.camRotationX, -object.camRotationY, 0), 1)
			elseif MouseDown(1) ~= 1 and object.leftMouseDown == true then
				MoveMouse(object.mouseX, object.mouseY)
				ShowMouse()
				object.leftMouseDown = false
			end

			t:Hide()
			object.cameraCube:Hide()	-- debug
			object.playerCube:Hide()	-- debug
			if EntityVisible(object.playerPivot, object.cameraPivot) == 0 then
				local p = LinePick(EntityPosition(object.playerPivot, 1), EntityPosition(object.cameraPivot, 1), 0, 0)
				if p ~= nil then
					PositionEntity(object.cameraPivot, Vec3(p.position.x, p.position.y, p.position.z), 1)
					PointEntity(object.cameraPivot, object.playerPivot, 3, 1, 0)
					MoveEntity(object.cameraPivot, Vec3(0, 0, .25))
				end
			end
			object.cameraCube:Show()	-- debug
			object.playerCube:Show()	-- debug
			t:Show()

			-- the pivot to follow the player around
			object.playerPivot:SetPosition(t:GetPosition())
			object.cameraPivot:Point(object.playerPivot, 3, 1, 0)
			fw.main.camera:SetMatrix(object.cameraPivot.mat)
		end
	end
end
end

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...