Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Newby | |
Old Version | Published before Godot 3 was released. |
This is my code
if btn_left.check()==2:
move(-player_speed, acceleration,delta)
ORIENTATION_NEXT="left"
anim="Run"
anim_speed=1.0
anim_blend=0.2
elif btn_right.check()==2:
move(player_speed, acceleration,delta)
ORIENTATION_NEXT="right"
anim ="Run"
anim_speed=1.0
anim_blend=0.2
elif block.check()==2:
move(0, acceleration,delta)
anim ="Block"
anim_speed=1.0
anim_blend=0.2
elif atk.check()==1:
move(0, acceleration,delta)
anim ="Attack"
anim_speed=1.0
anim_blend=0.2
elif anim!="Attack":
move(0, acceleration,delta)
anim="Idle"
anim_speed=1.0
anim_blend=0.2
We need to see the whole code. Is that code in the _process
method? How is the animation being handled? Maybe with that we can find whats wrong
quijipixel | 2017-10-01 03:03
This is my input state
class for input handling. Returns 4 button states
var input_name
var prev_state
var current_state
var input
var output_state
var state_old
Get the input name and store it
func _init(var input_name):
self.input_name = input_name
check the input and compare it with previous states
func check():
input = Input.is_action_pressed(self.input_name)
prev_state = current_state
current_state = input
state_old = output_state
if not prev_state and not current_state:
output_state = 0 ### Released
if not prev_state and current_state:
output_state = 1 ### Just Pressed
if prev_state and current_state:
output_state = 2 ### Pressed
if prev_state and not current_state:
output_state = 3 ### Just Released
return output_state
This is the base code
extends RigidBody2D
var input_states=preload(“res://Script/input_states.gd”)
#PLAYERVARIABLES
export var player_speed=200
export var acceleration=7
export var air_acceleration=1
export var extra_gravity=300
export var jump_force=200
#PLAYERSTATE
var PLAYERSTATE_PREV=“”
var PLAYERSTATE=“”
var PLAYERSTATE_NEXT=“ground”
var ORIENTATION_PREV=“”
var ORIENTATION=“”
var ORIENTATION_NEXT=“right”
#========================================
var raycast_down=null
var raycast_down2=null
var current_speed=Vector2()
var rotate=null
#CONTROLVARIABLES
var btn_right=input_states.new(“ui_right”)
var btn_left=input_states.new(“ui_left”)
var btn_up=input_states.new(“ui_up”)
var atk=input_states.new(“Attack”)
var block=input_states.new(“Block”)
var anim_player=null
var anim=“”
var anim_new=“”
var anim_speed=1.0
var anim_blend=0.2
#SPEED
func move(speed, acceleration,delta):
current_speed.x=lerp(current_speed.x,speed,acceleration*delta)
set_linear_velocity(Vector2(current_speed.x,get_linear_velocity().y))
#RAYCAST
func is_on_ground():
if raycast_down.is_colliding() or raycast_down2.is_colliding():
return true
else:
return false
func _ready():
raycast_down=get_node(“RayCast2D”)
raycast_down.add_exception(self)
raycast_down2=get_node(“2RayCast2D”)
raycast_down2.add_exception(self)
set_fixed_process(true)
set_applied_force(Vector2(0,extra_gravity))
rotate=get_node(“Rotate”)
anim_player=get_node(“Rotate/Character/AnimationPlayer”)
func rotate_behavior():
if ORIENTATION!=ORIENTATION_NEXT:
rotate.set_scale(rotate.get_scale()*Vector2(-1,1))
#CONTROLS
func _fixed_process(delta):
PLAYERSTATE_PREV=PLAYERSTATE
PLAYERSTATE=PLAYERSTATE_NEXT
ORIENTATION_PREV=ORIENTATION
ORIENTATION=ORIENTATION_NEXT
if PLAYERSTATE=="ground":
ground_state(delta)
elif PLAYERSTATE=="air":
air_state(delta)
if anim!=anim_new:
anim_new=anim
anim_player.play(anim,anim_blend,anim_speed)
func ground_state(delta):
if btn_left.check()==2:
move(-player_speed, acceleration,delta)
ORIENTATION_NEXT="left"
anim="Run"
anim_speed=1.0
anim_blend=0.2
elif btn_right.check()==2:
move(player_speed, acceleration,delta)
ORIENTATION_NEXT="right"
anim ="Run"
anim_speed=1.0
anim_blend=0.2
elif block.check()==2:
move(0, acceleration,delta)
anim ="Block"
anim_speed=1.0
anim_blend=0.2
elif atk.check()==1:
move(0, acceleration,delta)
anim ="Attack"
anim_speed=1.0
anim_blend=0.2
elif anim!="Attack":
move(0, acceleration,delta)
anim="Idle"
anim_speed=1.0
anim_blend=0.2
rotate_behavior()
if is_on_ground():
if btn_up.check()==1:
set_axis_velocity(Vector2(0,-jump_force))
else:
PLAYERSTATE_NEXT="air"
func air_state(delta):
if btn_left.check()==2:
move(-player_speed, air_acceleration,delta)
ORIENTATION_NEXT="left"
elif btn_right.check()==2:
move(player_speed, air_acceleration,delta)
ORIENTATION_NEXT="right"
if get_linear_velocity().y>0:
anim="Jump_down"
else:
anim="Jump"
rotate_behavior()
if is_on_ground():
PLAYERSTATE_NEXT="ground"
Newby | 2017-10-01 07:07