Character looks at my cursor, but the thing is that my character is rotated not only on the Y axis and lies on the ground (like sleeping lol).
I need the player to look at cursor_pos while aiming.
$Armature.look_at(cursor_pos, Vector3(0,1,0))
This is the code I'm using.
extends KinematicBody
var direction = Vector3.FORWARD
var velocity = Vector3.ZERO
var vertical_velocity = 0
var gravity = 20
var movement_speed = 0
var walk_speed = 1.5
var run_speed = 4
var acceleration = 8
var angular_acceleration = 9
var roll_magnitude = 23
var cursor_pos = Vector3.ZERO
func _ready():
pass
#Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
func look_at_cursor():
var player_pos = global_transform.origin
var drop_plane = Plane(Vector3(0,1,0), player_pos.y)
var ray_lenght = 1000
var mouse_pos = get_viewport().get_mouse_position()
var from = $Camera.project_ray_origin(mouse_pos)
var to = from + $Camera.project_ray_normal(mouse_pos) * ray_lenght
cursor_pos = drop_plane.intersects_ray(from, to)
$cursor.global_transform.origin = cursor_pos + Vector3(0,1,0)
func _input(event):
if event.is_action_pressed("sprint"):
if $roll_window.is_stopped():
$roll_window.start()
if event.is_action_released("sprint"):
if !$roll_window.is_stopped():
velocity = direction * roll_magnitude
$roll_window.stop()
$roll_timer.start()
func _physics_process(delta):
if !$roll_timer.is_stopped():
acceleration = 10
else:
acceleration = 15
if Input.is_action_pressed("ui_up") || Input.is_action_pressed("ui_down") || Input.is_action_pressed("ui_left") ||Input.is_action_pressed("ui_right"):
direction = Vector3(Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
0,
Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")).normalized()
if Input.is_action_pressed("sprint"):
movement_speed = run_speed
else:
movement_speed = walk_speed
else:
movement_speed = 0
velocity = lerp(velocity, direction * movement_speed, delta * acceleration)
move_and_slide(velocity + Vector3.DOWN * vertical_velocity, Vector3.UP)
look_at_cursor()
if !is_on_floor():
vertical_velocity += gravity * delta
else:
vertical_velocity = 0
if Input.is_action_pressed("aim"):
$Armature.look_at(cursor_pos, Vector3(0,1,0))
else:
$Armature.rotation.y = lerp_angle($Armature.rotation.y, atan2(direction.x * -1, direction.z * -1), delta * angular_acceleration)