0 votes

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
    elif Input.is_action_pressed("ui_left"):
        sprite.flip_h = true
        motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
        motion.x = 0

    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.

in Engine by (12 points)
recategorized by

1 Answer

0 votes

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

by (1,022 points)

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

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
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.