Jump to content

Movement Broken (Went too deep?) - Top Down View


WazMeister
 Share

Recommended Posts

Hi All (again).

 

I must of got too cocky with programming my movement, I wanted to get an understanding of diffrent approaches and implement a state etc.  I used the Darkness awaits player template for movement and tweaked it for my liking.. to learn and understand.  It never really worked, player just moved on the spot and animation played. AFter playing some more, I strted refrencing the tird person script for help and understanding.... then finally back to my original code I used before (which sort of worked).  Now i have the player turning on the spot (the effect I want) but he still wont move at all.. just plays animation for forward and back!

I thought I understood the code, but now I'm ripping my hair out with frustration, Tried using entity:Move command and entity:SetInput but it just wont fix it!

Would someone be able to review my code below and see where I am going wrong?!?!?

 

local runmultiplier = 0.8

--Set Script properties / define variables
Script.health = 100--"Health
Script.maxHealth = 100
Script.WalkSpeed = .02
Script.RunSpeed = 0.1
Script.maxAcceleration = 1.0--float "Max Acceleration"

--Player sates (Setup in a lua table with fields)
Script.state = {} --Table called state
--Add fields to table
Script.state.idle = 0
Script.state.walk = 1
Script.state.run = 2
Script.state.carry = 3
Script.state.dead = 4




function Script:Start()
	--Set the type to player,  keyvalue is string value associated with a key name
	self.entity:SetKeyValue("type", "player")
	
	if self.entity:GetMass() == 0 then
		self.entity:SetMass(10)
	end

	self.angle = self.entity:GetRotation().y
	self.entity:SetPhysicsMode(Entity.CharacterPhysics) --Setup character controller physics mode
	self.entity:SetCollisionType(Collision.Character, true)

	--Create a camera to view game/screen.
	self.camera = Camera:Create()
	-- Leave for example changing Field of View
	--self.camera:SetFov(70)
	--Set the antialias of camera (MSAA)
	self.camera:SetMultisampleMode((System:GetProperty("multisample", "1")))
	
	--Get the players current position
	local playerPosition = self.entity:GetPosition()
	local playerRotation = self.entity:GetRotation()
	--Set camreas position to the player + above him
	self.camera:SetPosition(playerPosition.x, playerPosition.y + 3, playerPosition.z)
	--Rotate the camrea for looking down on player (Top Down View)
	self.camera:SetRotation(Vec3(90, 0, 0))
	
	--[[
	Set script to refer to for the camrea movement etc. set to false so start function won't start within.
	self.camera:SetScript("Scripts/Objects/Camrea/3rdPersonFollow.lua", false)
	set target varible in the 3rdperson script referenced as this entity (i.e the player)
	self.camera.script.target = self.entity
	self.camera.script:Start()
	]]--

	--setup lua table to hold healthbar images
	self.image = {}
	self.image.healthbar = Texture:Load("Materials/HUD/healthbar.tex")
	self.image.healthmeter = Texture:Load("Materials/HUD/healthmeter.tex")
	
	self.image={}
	self.image.healthbar = Texture:Load("Materials/HUD/healthbar.tex")
	self.image.healthmeter = Texture:Load("Materials/HUD/healthmeter.tex")

	--Define default state variables
	self.currentState = -1
end

function Script:UpdatePhysics()
	--blank vec3 variable for later adding key presses movement.x, movement.y and movement.z
	local movement = Vec3(0)
	--Get the Game Window
	local window = Window:GetCurrent()
	local changed
	local move = 0
	local prevState = self.currentState
	
	
	--Check state for player dead
	if self.currentState == self.state.dead or self.health <= 0 then return end
	
	--Check if carrying, if not then player will be idle state
	if self.currentState ~= self.state.carry then
		self.currentState = self.state.idle
	end

	--Detect if grabbing a bin
	if self.currentState ~= self.state.carry then
		local docarry = false
		if window:KeyDown(Key.Space) then 
			self.currentState = self.state.carry
			--self.attackstarttime = Time:GetCurrent()
		end
	end

	--Movement
	--Code for detecting key presses for movement and attacks
	if window:KeyDown(Key.W) then movement = movement + 1 changed = true end
	if window:KeyDown(Key.S) then movement = movement - 1 changed = true end
	if changed then --If player is basically moving.. then do....
		if self.currentState ~= self.state.carry then
			--if window:KeyDown(Key.Shift) then
				--movement = movement:Normalize() * self.RunSpeed --Normalize vector to length of 1, so 1 * Runspeed 
				movement = movement * self.RunSpeed
				self.currentState = self.state.run
			--else
				movement = movement * self.WalkSpeed
				--movement = movement:Normalize() * self.WalkSpeed
				self.currentState= self.state.walk			
			--end
		end
	end
	self.entity:SetInput(self.angle, move,0,0)

	if window:KeyDown(Key.D) then self.entity:Turn(0, 3, 0) changed = true end
	if window:KeyDown(Key.A) then self.entity:Turn(0, -3, 0) changed = true end

--Update animation
	if prevState ~= self.currentState then
		if self.currentState == self.state.idle then
			self.entity:PlayAnimation("Idle", .2, 200)
		elseif self.currentState == self.state.walk then
			self.entity:PlayAnimation("Walk", .1, 200)			
		elseif self.currentState == self.state.run then
			self.entity:PlayAnimation("Run", .2, 200)
		elseif self.currentState == self.state.carry then
		end	
	end
end

 

Dream since child of making games! From Game Programming Starter Kit 3.0, Blitz Basic, Map Creation since Duke 3D, Game Maker, Blitz3D (of recent..2023) and many other engines and years..... never really sticking to it with inner struggles that I've had to fight along with pushing to learn and acheive.

40 years old.. came across Leadwerks on Steam... Learning slowly but surely and loving it!

Learn with me or just watch me fail! at my random Youtube Channel, as I stream adhoc while learning and using LeadWerks and other game creating tools.

https://www.youtube.com/@wazmeister3151/featured

Link to comment
Share on other sites

Got it working by changing the movement code  around and likely more basic...  Not sure if it's the right way or best way to do this, but it works I guess...

unction Script:UpdatePhysics()
	--blank vec3 variable for later adding key presses movement.x, movement.y and movement.z
	local movement = 0
	--Get the Game Window
	local window = Window:GetCurrent()
	local changed

	--Check state for player dead
	if self.currentState == self.state.dead or self.health <= 0 then return end
	
	--Check if carrying, if not then player will be idle state
	if self.currentState ~= self.state.carry then
		self.currentState = self.state.idle
	end

	--Detect if grabbing a bin
	if self.currentState ~= self.state.carry then
		local docarry = false
		if window:KeyDown(Key.Space) then 
			self.currentState = self.state.carry
			--self.attackstarttime = Time:GetCurrent()
		end
	end

	--Movement
	--Code for detecting key presses for movement and attacks
	if window:KeyDown(Key.W) then movement = movement - 1 changed = true end
	if window:KeyDown(Key.S) then movement = movement + 1 changed = true end
	if window:KeyDown(Key.D) then self.entity:Turn(0, 3, 0) change = true end
	if window:KeyDown(Key.A) then self.entity:Turn(0, -3, 0) change = true end
	if changed then
		if self.currentState ~= self.state.carry then
			if window:KeyDown(Key.Shift) then
				movement = movement * self.RunSpeed
				self.currentState = self.state.run
			else
			movement = movement * self.WalkSpeed
			self.currentState = self.state.walk
			end
		end
	end

	self.entity:Move(0, 0, movement)

 

 

 

Dream since child of making games! From Game Programming Starter Kit 3.0, Blitz Basic, Map Creation since Duke 3D, Game Maker, Blitz3D (of recent..2023) and many other engines and years..... never really sticking to it with inner struggles that I've had to fight along with pushing to learn and acheive.

40 years old.. came across Leadwerks on Steam... Learning slowly but surely and loving it!

Learn with me or just watch me fail! at my random Youtube Channel, as I stream adhoc while learning and using LeadWerks and other game creating tools.

https://www.youtube.com/@wazmeister3151/featured

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...