Jump to content

Headbobbing Mechanic


SlipperyBrick
 Share

Recommended Posts

I am looking to add headbobbing motion to my player. I have a custom script for my player which nick.ace helped code.

 

When I have modelled my character for the player I will be doing a full body view so when you look down you will see the player model rather than just a floating pair of arms.

 

I am wondering how I can add headbobbing but also not have any clipping issues when I add my player model in. I can imagine I can parent the player camera to the head bone of my player which should follow the movement of my player so rather than coding the headbobbing in would it be possible to just animate my player with a headbob and parent the player camera to the head bone.

 

If the above isn't efficient could the headbobbing be coded in my player script. For anyone interested in my player script here it is so you can look at it. It isn't finished yet, I am hoping to do lean and peek pretty much the same way lean and peek is managed in Alien: Isolation, they do it in a very intuitive way.

 

function Script:Start()

local window = Window:GetCurrent()

--self.camera = players camera
--self.entity = temporary box to use for player movement

--creates a camera in the scene and parents to the entity

self.camera = Camera:Create(self.entity)

--sets the type of the object to player

self.entity:SetKeyValue("type", "player")
local mass = self.entity:GetMass()
if self.entity:GetMass() == 0 then
Debug:Error("Player mass should be greater than 0.")

end

--gets the entity position

local entpos = self.entity:GetPosition(true)

--sets the camera height

self.camera:SetPosition(entpos.x, entpos.y + 3.5, entpos.z, true)

--sets the camera rotation variable

self.camrotation = self.entity:GetRotation(true)
self.camera:SetRotation(self.camrotation.x, self.camrotation.y, 0, true)
mousex = Math:Round(window:GetWidth() / 2)
mousey = Math:Round(window:GetHeight() / 2)

--sets antialiasing samples

self.camera:SetMultisampleMode(4)

end

function Script:UpdateWorld()

local window = Window:GetCurrent()

--gets the position of the mouse

local mouseposition = window:GetMousePosition()

window:SetMousePosition(mousex, mousey)

local dx = mouseposition.x - mousex
local dy = mouseposition.y - mousey

--sets the mouse speed, higher values = less speed

self.camrotation.x = self.camrotation.x + dy / 10 --x value
self.camrotation.y = self.camrotation.y + dx / 10 --y value
self.camera:SetRotation(self.camrotation.x, self.camrotation.y, 0, true)

end

function Script:UpdatePhysics()

local window = Window:GetCurrent()

--xmove = left/right speed
--zmove = forward/backwards speed

local xmove = 0
local zmove = 0

--key bindings for movement

if window:KeyDown(Key.W) then
zmove = 2

end

if window:KeyDown(Key.S) then
zmove = -2

end

if window:KeyDown(Key.A) then
xmove = -2

end

if window:KeyDown(Key.D) then
xmove = 2

end

--sets the movement parameters of the entity

self.entity:SetInput(self.camera:GetRotation(true).y, zmove, xmove)

end

Link to comment
Share on other sites

I am looking for some help on how to clamp the camera movement on the Y axis.

 

I can do a full 360 turn on the Y axis when I look up and down.

 

My script is pasted above it is fairly basic, I can't seem to figure out though how to do this. I referenced the FPSPlayer script but couldn't find anything that would help me with this (unless I am not looking close enough).

 

I'm hoping it is possible to do this angle based rather than using coordinates to work it out. I've been looking over the API reference here but feeling a little lost on where to start to work this out haha.

 

EDIT: Sorry if my script looks a little weird. I don't use indents as I struggle to read the code when there is indents, not the norm I know haha but I'll slowly ease myself in to that so I can get used to it.

 

Hopefully though you guys can read it ok and make sense of it.

Link to comment
Share on other sites

to clamp the rotation, use the max and min on the camera's pitch value:

 

window = Window:Create()

context = Context:Create(window)

world = World:Create()

camera = Camera:Create()

camera:Move(0,2,0)

skybox = Texture:Load("Materials/Sky/skybox_texture.tex")

camera:SetSkybox(skybox)

light = DirectionalLight:Create()

light:SetRotation(35,-35,35)

 

ground = Model:Box(10,1,10)

ground:SetPosition(0,-0.5,0)

ground:SetColor(0.0,0.25,0.0)

box = Model:Box()

 

camrot = camera:GetRotation()

gx=Math:Round(context:GetWidth()/2)

gy=Math:Round(context:GetHeight()/2)

 

while window:KeyDown(Key.Escape)==false do

if window:Closed() then return false end

 

local mouseposition = window:GetMousePosition()

local dx = mouseposition.x - gx

local dy = mouseposition.y - gy

camrot.x = camrot.x + dy / 10.0

 

camrot.x = math.min(camrot.x,90)

camrot.x = math.max(camrot.x,-90)

 

camrot.y = camrot.y + dx / 10.0

camera:SetRotation(camrot)

window:SetMousePosition(gx,gy)

 

world:Update()

world:Render()

context:Sync(true)

end

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

I would like to see this headbobbing, I have a fear that it might become irritating if it is ON all the time, a bit like the hard sound of footsteps when the player walks on grass.Just my opinion, could be wrong.

  • Upvote 1

amd quad core 4 ghz / geforce 660 ti 2gb / win 10

Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++

Link to comment
Share on other sites

I agree with that.

 

I think the headbobbing mechanic may need to be something that is coded into my Player script rather than something that has been baked in to my animations.

 

@macklebee. Math:Min and Math:Max works great by the way. I need to tweak some stuff in the script to make mouse movement a little more smoother because when the mouse hits the angles I've coded in to the min and max functions the camera starts getting a bit jerky when you continue to move the mouse up or down. Best way I can describe it is it's like a shaky kind of effect when you try to go past those angles I've coded in the script.

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