Character mesh rotates completely rotates when the character is in an specific angle

Godot Version

3.5.3

Question

Hi, i"m new here and i want to know a solution to this problem that the character take a 360 degs rotation when is in a specific angle here is the character script:

extends KinematicBody

export var mouse_sensivity = 0.1

export var min_camera_angle_limit = -60.0
export var max_camera_angle_limit = 60.0

export var speed = 7.0
export var jump_strength = 20.0
export var gravity = 50.0

var rotation_speed = 7

var velocity = Vector3.ZERO
var snap_vector = Vector3.DOWN

onready var camera_pivot = $camera_pivot
onready var model = $player_Model

func _ready() -> void:
	Input.set_mouse_mode(2)
	
func _input(event: InputEvent) -> void:

	if Input.is_action_just_released("ui_cancel") and Input.mouse_mode == 2:
		Input.set_mouse_mode(0)
	elif Input.is_mouse_button_pressed(1) and Input. mouse_mode == 0:
		Input.set_mouse_mode(2)
		
	if event is InputEventMouseMotion:
		camera_pivot.rotation_degrees.x -= event.relative.y * mouse_sensivity
		camera_pivot.rotation_degrees.x = clamp(camera_pivot.rotation_degrees.x, min_camera_angle_limit, max_camera_angle_limit)
	
		camera_pivot.rotation_degrees.y -= event.relative.x * mouse_sensivity
		camera_pivot.rotation_degrees.y = wrapf(camera_pivot.rotation_degrees.y, 0.0, 360.0)

func _physics_process(delta: float) -> void:
	var direction = Vector3.ZERO
	
	var move_foreward = Input.get_action_strength("move_forward")
	var move_backwards = Input.get_action_strength("move_backward")
	var move_left = Input.get_action_strength("move_left")
	var move_right = Input.get_action_strength("move_right")
	
	if move_foreward:
		direction -= transform.basis.z

	if move_backwards:
		direction += transform.basis.z

	if move_left:
		direction -= transform.basis.x

	if move_right:
		direction += transform.basis.x

	direction = direction.rotated(Vector3.UP, camera_pivot.rotation.y).normalized()
	
	velocity.x = direction.x * speed
	velocity.z = direction.z * speed
	velocity.y -= gravity * delta
	
	var just_landed = is_on_floor() and snap_vector == Vector3.ZERO
	var is_jumping = is_on_floor() and Input.is_action_pressed("jump")
	
	if is_jumping:
		velocity.y = jump_strength
		snap_vector = Vector3.ZERO
		
	elif just_landed:
		snap_vector = Vector3.DOWN
		
	velocity = move_and_slide_with_snap(velocity, snap_vector, Vector3.UP, true)
	
	if direction.length() > 0.2:
		var look_direction = -Vector2(velocity.z, velocity.x)
		var target_angle = look_direction.angle()
		var current_angle = model.rotation.y
		model.rotation.y = lerp(current_angle, look_direction.angle(), rotation_speed * delta)
		print(rad2deg(look_direction.x),rad2deg(look_direction.y) )

I think that this line has the fault:

	if velocity.length() > 0.2:
		var look_direction = -Vector2(velocity.z, velocity.x)
		var target_angle = look_direction.angle()
		var current_angle = model.rotation.y
		model.rotation.y = lerp(current_angle, look_direction.angle(), rotation_speed * delta)
		print(rad2deg(look_direction.x),rad2deg(look_direction.y))

I think is because i’m using a Vector2 and i’m converting velocity.z and x to radians and when the radians reaches in some value then the mesh rotates

Ok i solved the problem that the character completely rotates, other person told me that instead of using lerp() use lerp_angle().

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