working force push with raycast
This commit is contained in:
1 parent
25288b722b
commit
08a1145c50
5 files changed
+106
-18
No files matched your search
@@ -3,7 +3,17 @@ extends CharacterBody2D
|
||||
|
||||
const SPEED = 300.0
|
||||
const JUMP_VELOCITY = -450.0
|
||||
@export var PUSH_FORCE = 90
|
||||
@export var BUMP_FORCE = 90
|
||||
|
||||
# Shove Attack Variables
|
||||
@onready var right_ray: RayCast2D = $RightRay
|
||||
@onready var left_ray: RayCast2D = $LeftRay
|
||||
var faceLeft = false
|
||||
var pushTarget
|
||||
var pushRightEnabled = false
|
||||
var pushLeftEnabled = false
|
||||
@export var PUSH_FORCE = 700
|
||||
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
# Add the gravity.
|
||||
@@ -11,19 +21,48 @@ func _physics_process(delta: float) -> void:
|
||||
velocity += get_gravity() * delta
|
||||
|
||||
# Handle jump.
|
||||
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
|
||||
if Input.is_action_just_pressed("jump") and is_on_floor():
|
||||
velocity.y = JUMP_VELOCITY
|
||||
|
||||
# Shove Attack
|
||||
if Input.is_action_just_pressed("shove"):
|
||||
if pushRightEnabled && faceLeft == false:
|
||||
pushTarget.apply_central_impulse(Vector2(1,0)* PUSH_FORCE * 2)
|
||||
if pushLeftEnabled && faceLeft == true:
|
||||
pushTarget.apply_central_impulse(Vector2(-1,0)*PUSH_FORCE * 2)
|
||||
|
||||
# Get the input direction and handle the movement/deceleration.
|
||||
# As good practice, you should replace UI actions with custom gameplay actions.
|
||||
var direction := Input.get_axis("ui_left", "ui_right")
|
||||
var direction := Input.get_axis("left", "right")
|
||||
if direction:
|
||||
velocity.x = direction * SPEED
|
||||
if direction <0:
|
||||
faceLeft = true
|
||||
if direction >0:
|
||||
faceLeft = false
|
||||
else:
|
||||
velocity.x = move_toward(velocity.x, 0, SPEED)
|
||||
|
||||
move_and_slide()
|
||||
if right_ray.is_colliding():
|
||||
if not faceLeft:
|
||||
var collider = right_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
pushRightEnabled = true
|
||||
else:
|
||||
pushRightEnabled = false
|
||||
|
||||
if left_ray.is_colliding():
|
||||
if faceLeft:
|
||||
var collider = left_ray.get_collider()
|
||||
if collider is RigidBody2D:
|
||||
pushTarget = collider
|
||||
pushLeftEnabled = true
|
||||
else:
|
||||
pushLeftEnabled = false
|
||||
|
||||
for i in get_slide_collision_count():
|
||||
var c = get_slide_collision(i)
|
||||
if c.get_collider() is RigidBody2D:
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * PUSH_FORCE)
|
||||
c.get_collider().apply_central_impulse(-c.get_normal() * BUMP_FORCE)
|
||||
@@ -10,10 +10,9 @@ func _ready() -> void:
|
||||
timer.wait_time = 1
|
||||
timer.one_shot = false
|
||||
timer.connect("timeout", secondCounter)
|
||||
timer.start()
|
||||
# timer.start()
|
||||
|
||||
func secondCounter():
|
||||
print("one second")
|
||||
countdown -=1
|
||||
if countdown <=0:
|
||||
print("YOU LOSE")
|
||||
|
||||
Reference in new issue
Block a user