KinematicBody2D sprite rotating with 360 mouse aim. Help

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sensei_godot

Hi, I’m currently struggling to correct my code. I looking to get info on how to have my character shoot 360 with mouse but without the sprite being rotated. when mouse aim is from 270 to 90 (180°) face right and left when is 91 to 269 (180°) face left. Below is GIF representing my current character

Godot KinematicBody2D 360° shooting

Below my code:

KinematicBody2D GDScript (player scene)

extends KinematicBody2D

onready var can_shoot = false
onready var bullet_cointainer = get_node("bullet_container")
onready var sprite = $sprite

const UP = Vector2(0, -1)
const GRAVITY = 50
const ACCELERATION = 400
const MAX_SPEED = 600
const JUMP_HEIGHT = -950

var motion = Vector2()

func _ready():

func _physics_process(delta):
    motion.y += GRAVITY
    var friction = true

    if Input.is_action_pressed("ui_right"):
	    motion.x = min(motion.x+ACCELERATION, MAX_SPEED)
	    sprite.flip_h = false"running")
    elif Input.is_action_pressed("ui_left"):
	    sprite.flip_h = true"running")
	    motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
	    motion.x = 0"idle")

    if Input.is_action_pressed("player_shoot"):
	    var player_aim_angle
	    player_aim_angle = get_angle_to(get_global_mouse_position())

    if is_on_floor():
	    if Input.is_action_just_pressed("ui_select"):
		    motion.y = JUMP_HEIGHT
	    if friction == true:
		    motion.x = lerp(motion.x, 0, 0.2)
	    if friction == true:
		    motion.x = lerp(motion.x, 0, 0.2)
	    if Input.is_action_pressed("ui_left") and not Input.is_action_pressed("ui_right"):
		    sprite.scale.x = -1
	    if Input.is_action_pressed("ui_right") and not Input.is_action_pressed("ui_left"):
		    sprite.scale.x = 1
    motion = move_and_slide(motion, UP)

func shoot():
    if can_shoot:
        can_shoot = false
	    var bullet = preload("res://Scenes/player_bullet.tscn").instance()
	    var b = bullet
	    b.start($sprite/bullet_spawn.get_global_position(), get_rotation())

func _on_GunTimer_timeout():
    can_shoot = true

Area2D GDScript (bullet scene)

extends Area2D

export (float) var bullet_speed_multiplier = 1.2

export var speed = 1100
var velocity = Vector2()

func _ready():

func start(pos, dir):
    position = pos
    rotation = dir
    #velocity = dir * speed
    velocity = Vector2(speed, 0).rotated(dir)

func _physics_process(delta):
   position += velocity * delta * bullet_speed_multiplier

func _on_lifetime_timeout():

I going to the godot documents but I’m having difficulty with what to use to get the player to not rotate.
TL:DR Looking for help to get my Character to stop rotating with mouse aim and just basically have turn -x or +x depending where the move is poiting. I would probably need a sprite to act as a gun child or non child or kinematicbody

Thanks in advance.

:bust_in_silhouette: Reply From: Bartosz

Im using this piece of code to make sprite never rotate:

func _process(delta):
	sprite.rotation_degrees = -rigid_body.rotation_degrees

You can adapt it to your case by settien scale.x to -1 and 1 based on where players looks

Thanks for looking at this question. Could you elaborate how to do the scale

sensei_godot | 2018-03-29 01:24

To make sprite look left and right depending on rotation of rigid body you could do something like this

if rigid_body.rotation_degrees > -90 and rigid_body.rotation_degrees < 90:
    sprite.scale.x = 1
    sprite.scale.x = -1

Bartosz | 2018-03-29 19:36