Need to declare 2 things in one function for controller camera to work?

Using godot 4.2.2

I am implementing controller controls and in order to get my camera to turn when held smoothly as far as i know it have to be in the _process func but i already have delta declared in it how do i declare both delta and event in one func?

Sorry if this is a simple fix

What is event referencing in the if statement?

  • You could declare event at the start of the script, maybe as null.

But maybe you don’t need event?

Do you have actions defined for the twist_input events? you want to use the InputMap just like you did for actions “move_right” “move_left” etc. Then it will be similar to the get_axis code.

twist_input = Input.get_axis("twist_left", "twist_right") * controller_sensitivity
pitch_input = Input.get_axis("pitch_down", "pitch_up") * controller_sensitivity
1 Like

From what you said @gertkeno i got this which does turn my camera but weirdly enough only smoothly when im moving my character so when i stop moving it’s slow and jitters a bit even if im holding the stick in the direction it should just keep rotating even when stationary and i dont know why it isn’t.

You put this sample inside _unhandled_input which only runs when there is a change in input, this works great for the mouse motion, but not stick inputs. Place the sample inside _process.

And how do i declare both delta and event at once in the same function???

image

It doesn’t need if event ... Input works in any function, just like above for input.x and input.z

OH thank you so much haha there’s two other issues i noticed and I’m not sure why they messed up firstly the jumping on controller doesn’t work (my input is set correctly and everything)

And i have a wall_stick mechanic that sets gravity to zero when you touch the wall that has an on body entered, then you can jump off of it and now it doesn’t see to work and i don’t know why, any ideas.

Here’s the code relating to my character

extends RigidBody3D
class_name Player
#```
var respawn_position := global_position

var mouse_sensitivity := 0.001
var controller_sensitivity := 0.070
var twist_input := 0.0
var pitch_input := 0.0
var DASH_SPEED := 1000
var can_dash: bool = false
var walkingspeed= 1200
var crouchspeed = 600
var jumped = false
var doublejump = false

var currentspeed = walkingspeed

var isCrouching = false
var isNeutral = true
var isdashing = false
var groundpounding = false
var onground = false
var wallstick = false


@onready var character := $Character
@onready var twist_pivot := $TwistPivot
@onready var pitch_pivot := $TwistPivot/PitchPivot
@onready var timer = $dropTimer

func _ready() -> void:
	Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	
	
func _process(delta: float) -> void:
	
	var input := Vector3.ZERO
	if groundpounding == false:
		if wallstick == false:
			input.x = Input.get_axis("move_right", "move_left")
			input.z = Input.get_axis("move_back", "move_forward")
			apply_central_force(twist_pivot.basis * input * currentspeed * delta)
			#$Character/AnimationPlayer.play("Run")
		
	InputEventJoypadMotion
	twist_input = -Input.get_axis("look_left", "look_right") * controller_sensitivity
	pitch_input = -Input.get_axis("look_down", "look_up") * controller_sensitivity
	
	if $RayCast3D.is_colliding():
		if isCrouching == false:
			if wallstick == false:
				if isdashing == false:
					onground = true
					can_dash = true
	
	if Input.is_action_just_pressed("dash") and can_dash:
		if $RayCast3D.is_colliding():
			apply_central_impulse(Vector3.UP * 2.0)
			onground = true
			can_dash = false
			apply_central_impulse(twist_pivot.basis * input * DASH_SPEED * delta)
			timer.start()
			self.gravity_scale = 0
			isdashing = true
			$Character/AnimationPlayer.play("Dash")
			
		else:
			onground = true
			can_dash = false
			apply_central_impulse(twist_pivot.basis * input * DASH_SPEED * delta)
			timer.start()
			self.gravity_scale = 0
			isdashing = true
			$Character/AnimationPlayer.play("Dash")
		
	if Input.is_action_pressed("crouch"):
		if $RayCast3D.is_colliding() == false:
			if jumped == true:
				if isCrouching == false:
					$Character/AnimationPlayer.play("Ground Pound")
					apply_central_impulse(Vector3.DOWN * 3.0)
					groundpounding = true

		elif $RayCast3D.is_colliding():
			if isCrouching == false:
				movementStateChange("crouch")
				currentspeed = crouchspeed

		
	elif not Input.is_action_pressed("crouch") and $"Crouch Raycast".is_colliding() == false:
		if isCrouching == true:
			movementStateChange("uncrouch")
			currentspeed = walkingspeed


	if Input.is_action_just_pressed("ui_cancel"):
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		
	twist_pivot.rotate_y(twist_input)
	pitch_pivot.rotate_x(pitch_input)
	pitch_pivot.rotation.x = clamp(pitch_pivot.rotation.x, 
	deg_to_rad(-20), 
	deg_to_rad(20)
	)
	twist_input = 0.0
	pitch_input = 0.0
	
	if not input.is_zero_approx():
		var move_direction = twist_pivot.basis * input
		var align = character.transform.looking_at(character.transform.origin - move_direction)
		character.transform = character.transform.interpolate_with(align, delta * 20.0)

func _on_drop_timer_timeout():
	if wallstick == true:
		pass
	else:
		self.gravity_scale = 3.5
		isdashing = false


func _unhandled_input(event: InputEvent) -> void: 
	
	if event is InputEventMouseMotion:
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			twist_input = - event.relative.x * mouse_sensitivity
			pitch_input = event.relative.y * mouse_sensitivity
			
	
	

func _unhandled_key_input(event):

	if event.is_action_pressed("jump"):
		if $RayCast3D.is_colliding() or wallstick == true:
			apply_central_impulse(Vector3.UP * 16.0)
			self.gravity_scale = 3.5
			can_dash = true
			jumped = true
			wallstick = false
			if isCrouching == false:
				$Character/AnimationPlayer.play("Jump")
				
		elif jumped == true:
			if isCrouching == false:
				if doublejump == false:
					$Character/AnimationPlayer.play("Double Jump")
					apply_central_impulse(Vector3.UP * 16.0)
					self.gravity_scale = 3.5
					doublejump = true
					
	if $RayCast3D.is_colliding() or wallstick:
		doublejump = false
		groundpounding = false
					
					
		if $RayCast3D.is_colliding():
			groundpounding = false

func movementStateChange(changeType):
	match changeType:
		"crouch":
			if isNeutral:
				$Character/AnimationPlayer.play("Crouch")
			isCrouching = true
			isNeutral = false
			changeCollisionShapeTo("crouching")
			
		"uncrouch":
			$Character/AnimationPlayer.play_backwards("Crouch")
			isCrouching = false
			isNeutral = true
			changeCollisionShapeTo("standing")


func changeCollisionShapeTo(shape):
	match shape:
		"crouching":
			#false is enabled josh
			$crouchCollisionShape3D.disabled = false
			$standingCollisionShape3D.disabled = true
		
		"standing":
			$crouchCollisionShape3D.disabled = true
			$standingCollisionShape3D.disabled = false


func _on_wall_stick_zone_body_entered(body):
	if body is Player:
		if isdashing == true:
			wallstick = true
			$Character/AnimationPlayer.play("Wall Stick")
			self.gravity_scale = 0
			jumped = false

I would bet you don’t mean to check that body is Player. this would mean the player has entered the wall stick zone area, which I assume is a child of player and should only be colliding with walls. Maybe you mean to do the opposite not body is Player

No that didn’t fix it and cause i want to make sure the only thing that stick is like the class name player, it used to work before with this exact code don’t know why it isn’t anymore and just notice now the mouse camera isn’t working only controller

I’ll focus on the camera input since that’s what this thread is about.

You are storing the twist_input and pitch_input variables but I believe you would be better off applying the rotation immediately.

func apply_camera_input(twist_input: float, pitch_input: float) -> void:
	twist_pivot.rotate_y(twist_input)
	pitch_pivot.rotate_x(pitch_input)
	pitch_pivot.rotation.x = clamp(pitch_pivot.rotation.x, 
	deg_to_rad(-20), 
	deg_to_rad(20)
	)

func _unhandled_input(event: InputEvent) -> void: 
	if event is InputEventMouseMotion:
		if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
			var twist: float = - event.relative.x * mouse_sensitivity
			var pitch: float = event.relative.y * mouse_sensitivity
			apply_camera_input(twist, pitch)

func _process(delta: float) -> void:
	var input := Vector3.ZERO
	if not (groundpounding or wallstick):
		input.x = Input.get_axis("move_right", "move_left")
		input.z = Input.get_axis("move_back", "move_forward")
		apply_central_force(twist_pivot.basis * input * currentspeed * delta)
		#$Character/AnimationPlayer.play("Run")
		
	var twist_pitch: Vector2 = -Input.get_vector("look_left", "look_right", "look_down", "look_up") * controller_sensitivity
	apply_camera_input(twist_pitch.x, twist_pitch.y)

(Edit: Thanks so much for the help it seems to be all good now!)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.