CharacterBody3D Shakeing, vibrating

Godot version 4.3

Hi everyone, im working on a TPS game and enemy characters are shakeing , vibrating , they do the animation and do fine everything but vibrate and dont know why, interesting , if i open other project and put in those modells and node files , they work fine and no shakeing, please help me ,how can i stop this vibrate?

Share the script of this Node please


@onready var health_manager: Node3D = $HealthManager
@export var animation_player : AnimationPlayer
@onready var vision_manager: VisionManager = $VisionManager
@onready var ai_character_mover: Node3D = $AICharacterMover
@onready var attack_emitter: BulletEmitter = $AttackEmitter

@onready var nearby_monsters_alert_area: Area3D = $NearbyMonstersAlertArea
@onready var player = get_tree().get_first_node_in_group("player")


enum STATES {IDLE, ATTACK, DEAD}
var cur_state = STATES.IDLE

@export var attack_range = 2.0
@export var damage = 15

@export var attack_speed_modifier = 1.0

func _ready():
	var hitboxes = find_children("*", "HitBox")
	for hitbox in hitboxes:
		hitbox.on_hurt.connect(health_manager.hurt)
	health_manager.died.connect(set_state.bind(STATES.DEAD))
	health_manager.gibbed.connect(queue_free)
	hitboxes.append(self)
	attack_emitter.set_bodies_to_exclude(hitboxes)
	attack_emitter.set_damage(damage)
	
	set_state(STATES.IDLE)
	
func hurt(damage_data: DamageData):
	health_manager.hurt(damage_data)
	
func alert():
	if cur_state == STATES.IDLE:
		set_state(STATES.ATTACK)
		alert_nearby_monsters()
		
func alert_nearby_monsters():
	for b in nearby_monsters_alert_area.get_overlapping_bodies():
		if b is Bandit:
			b.alert()
		
	

func set_state(state: STATES):
	if cur_state == STATES.DEAD:
		return
	cur_state = state
	match cur_state:
		STATES.ATTACK:
			print("ATTACK STATE SET")
		STATES.IDLE:
			animation_player.play("idle", -1, 0.2)
		STATES.DEAD:
			animation_player.play("dead" ,0.2)
			collision_layer = 0
			collision_mask = 1
			ai_character_mover.stop_moving()

func _process(delta):
	match cur_state:
		STATES.IDLE:
			process_idle_state(delta)
		STATES.ATTACK:
			process_attack_state(delta)

func process_idle_state(_delta):
	if vision_manager.can_see_target(player):
		alert()
	#print(vision_manager.can_see_player())

func process_attack_state(_delta):
	var attacking = animation_player.current_animation == "attack"
	var vec_to_player = player.global_position - global_position
	
	if vec_to_player.length() <= attack_range:
		ai_character_mover.stop_moving()
		if !attacking and vision_manager.is_facing_target(player):
			animation_player.play("attack")
		elif !attacking:
			ai_character_mover.set_facing_dir(vec_to_player)
	elif !attacking:
		ai_character_mover.set_facing_dir(ai_character_mover.move_dir)
		ai_character_mover.move_to_point(player.global_position)
		animation_player.play("run", -1, 0.8)

func start_attack():
	animation_player.play("attack", -1, attack_speed_modifier)

func do_attack():
	attack_emitter.fire()```

The Ai CharacterMover script :


@export var turn_speed = 300.0
var facing_dir : Vector3
@onready var navigation_agent_3d: NavigationAgent3D = $NavigationAgent3D
var moving = false


func _ready():
	super()
	facing_dir = -character_body.global_transform.basis.z
	

func set_facing_dir(new_face_dir: Vector3):
	facing_dir = new_face_dir
	facing_dir.y = 0.0

func move_to_point(point: Vector3):
	moving = true
	navigation_agent_3d.target_position = point
	
func stop_moving():
	moving = false
	set_move_dir(Vector3.ZERO)

func _physics_process(delta):
	super(delta)
	#MOVEMENT
	if moving:
		set_move_dir(navigation_agent_3d.get_next_path_position() - global_position)
	
	#FACEING
	var fwd = -character_body.global_transform.basis.z
	var right = character_body.global_transform.basis.x
	var angle_diff = fwd.angle_to(facing_dir)
	var turn_dir = 1
	if right.dot(facing_dir) > 0:
		turn_dir = -1
	
	var turn_amnt = delta * deg_to_rad(turn_speed)
	if turn_amnt < angle_diff:
		turn_amnt = angle_diff
		
	character_body.global_rotation.y += turn_amnt * turn_dir```

You pasted 2x times the same script.
And please use preformatted text ``` when pasting code.

Sorry, edited

Will the shaking be stopped if you removed (or commented out) this line from your code?

I know your enemy will not rotate when you remove this line, I’m just trying to pin-point which piece of code is the rootcause of the problem.

2 Likes

Ahh yes you right the shaking has stoped , but now they cant rotate :smiley:

1 Like

Can you try replacing all of this code:

with this code:

func _physics_process(delta):
	super(delta)
	#MOVEMENT
	if moving:
		set_move_dir(navigation_agent_3d.get_next_path_position() - global_position)
	
	#FACEING
	global_rotation.y = lerp_angle(global_rotation.y, facing_dir.y, turn_speed)

Let me know if that works for you, or if you have any issues.

Thank you for the helping , i try the code what you wrote it , the shakeing is stoped but dont do the rotation :frowning:

Maybe your state machine is not working correctly then? Try adding some print statements to see if facing_dir is set correctly.

the print state say this :

maybe overwrite the “turn_dir value”

There is no turn_dir variable anymore, I removed it.
This print statement with Vector3(0, 0, -1) is just what you set in the _ready() function.

Try adding print statements around the places that you call set_facing_dir() method to see if the direction is changed at all.

2 Likes

Thank you very much for helping! i rotate the vision_manager , and ai_character_mover , nodes to 180° and its faceing now the right direction, and the vibration is gone , thank you. :slight_smile:

1 Like