Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | jGames20 |
So, I am working on my first 3D game with the Godot Engine and I’ve set up a First Person Controller similar to that of the tutorial “Godot 101: Intro to 3D (part 7): First-person Character” by the KidsCanCode YouTube Channel. Albeit with a few modifications to enable some basic game pad support.
One of the features I would love to add to my game is a head bob/view bob whenever the player walks or runs (I haven’t added running functionality yet). I was searching with my default search engine for any resources to help me understand how the mechanic would work and I found this forum question on the gamedev stackexchange here:
Although the answer is in Unity C#, and I’m don’t think I’m converting it correctly. It keeps giving me errors with the t var and when I try to += the bobOscillate var it also throws errors on that. Are there some resources to help me learn how to properly do this stuff? Also, one thing that isn’t explained in the stackexchange post is what and where I assign to the time variable.
I’m kind of new to Godot so bear with me on this please.
Here is my scripts so far:
extends KinematicBody
onready var camera = $Pivot/Camera
var gravity = -ProjectSettings.get_setting("physics/3d/default_gravity")
var jump_speed = 4.9
var max_speed = 8
var mouse_sensitivity = 0.002 # Radians/Pixel
var gpDelta = 0
const JOY_DEADZONE = 0.2
const JOY_AXIS_RESCALE = 1.0/(1.0-JOY_DEADZONE)
const JOY_ROTATION_MULTIPLIER = 200.0 * PI / 180.0
var velocity = Vector3()
func get_input():
var input_dir = Vector3()
if Input.is_action_pressed("move_forward"):
input_dir += -camera.global_transform.basis.z
if Input.is_action_pressed("move_backward"):
input_dir += camera.global_transform.basis.z
if Input.is_action_pressed("move_left"):
input_dir += -camera.global_transform.basis.x
if Input.is_action_pressed("move_right"):
input_dir += camera.global_transform.basis.x
input_dir = input_dir.normalized()
return input_dir
func _unhandled_input(event):
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotate_y(-event.relative.x * mouse_sensitivity)
$Pivot.rotate_x(-event.relative.y * mouse_sensitivity)
$Pivot.rotation.x = clamp($Pivot.rotation.x, -1.2, 1.2)
func _physics_process(delta):
velocity.y += gravity * delta
var desired_velocity = get_input() * max_speed
velocity.x = desired_velocity.x
velocity.z = desired_velocity.z
velocity = move_and_slide(velocity, Vector3.UP, true)
#ApplyBob(delta)
if Input.get_connected_joypads().size() > 0:
var xAxis = Input.get_joy_axis(0, JOY_AXIS_2)
if abs(xAxis) > JOY_DEADZONE:
if xAxis > 0:
xAxis = (xAxis - JOY_DEADZONE) * JOY_AXIS_RESCALE
else:
xAxis = (xAxis + JOY_DEADZONE) * JOY_AXIS_RESCALE
rotate_object_local(Vector3.UP, -xAxis * delta * JOY_ROTATION_MULTIPLIER)
var yAxis = Input.get_joy_axis(0, JOY_AXIS_3)
if abs(yAxis) > JOY_DEADZONE:
if yAxis > 0:
yAxis = (yAxis - JOY_DEADZONE) * JOY_AXIS_RESCALE
else:
yAxis = (yAxis + JOY_DEADZONE) * JOY_AXIS_RESCALE
$Pivot.rotate_object_local(Vector3.RIGHT, -yAxis * delta * JOY_ROTATION_MULTIPLIER/2)
$Pivot.rotation.x = clamp($Pivot.rotation.x, -1.2, 1.2)
else:
return
func ApplyBob(time):
var t = $Pivot.translate
var vel = self.velocity
vel.y = 0 # 0 out y (up) component for xz velocity only
var velocityBob = vel.normalized()
if abs(velocityBob.z) <= 0.0001:
return;
var bobOscillate = sin(time * velocityBob.z * (2 * PI))
t.y += bobOscillate # Oscillate the position in y (up) axis
t.x += bobOscillate * 5 # Oscillate 5 degrees in each direction around x axis, presented this way for clarity.
I messed with the code for the oscillating part earlier and now I don’t know what I had that didn’t throw errors, but also didn’t really work either.