![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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():
pass
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
sprite.play("running")
elif Input.is_action_pressed("ui_left"):
sprite.flip_h = true
sprite.play("running")
motion.x = max(motion.x-ACCELERATION, -MAX_SPEED)
else:
motion.x = 0
sprite.play("idle")
$anim.play("idle")
if Input.is_action_pressed("player_shoot"):
shoot()
var player_aim_angle
player_aim_angle = get_angle_to(get_global_mouse_position())
self.rotate(player_aim_angle)
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)
else:
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
bullet_cointainer.add_child(b)
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():
set_physics_process(true)
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():
queue_free()
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.