Hello! I have rigidBody, that moving with force, and i have camera that rotates when your mouse position changing. Basically i want, when you press W your character move in the direction its facing. Here's my code:
var ammo = 0
var maxAmmo = 0
var ammoOnStock = 0
onready var camera = get_node("Camera")
onready var body = get_node("Body")
onready var bodyScale = body.scale
onready var collisionShape = get_node("CollisionShape")
onready var hand = camera.get_node("Hand")
onready var ui = get_node("UI")
func reload():
hand.rotation_degrees.x -= 30
yield(get_tree().create_timer(2), "timeout")
hand.rotation_degrees.x += 30
ammoOnStock -= maxAmmo
ammo += maxAmmo
updateAmmoUI()
func loadAmmo(stats):
maxAmmo = stats[0]
ammoOnStock = stats[1]
ammo = maxAmmo
func updateAmmoUI():
ui.get_node("Pistol").get_node("Ammo").text = str(ammo) + "/" + str(ammoOnStock)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _input(event):
if event is InputEventMouseMotion:
var mousePos = get_viewport().get_mouse_position()
camera.rotation_degrees = Vector3(mousePos.y*-1, mousePos.x*-1, 0)
body.rotation_degrees = Vector3(90, camera.rotation_degrees.y, 0)
if event is InputEventMouseButton:
print('mouse')
if usingWeapon == false:
print(usingWeapon);
if screenPointToRay():
usingWeapon = true
if ammo > 0:
ammo -= 1
updateAmmoUI()
var result = screenPointToRay()
if result.collider.name == "Enemy":
print("Hit body!")
result.collider.queue_free()
else:
print("Hit ", result.collider.name)
hand.rotation_degrees.x += 5
yield(get_tree().create_timer(0.2), "timeout")
hand.rotation_degrees.x -= 5
yield(get_tree().create_timer(0.3), "timeout")
else:
if ammoOnStock > 0:
reload()
yield(get_tree().create_timer(2), "timeout")
usingWeapon = false
func _physics_process(_delta):
print(camera.rotation_degrees)
if Input.is_action_pressed("player_forward"):
apply_central_impulse(Vector3.FORWARD)
if Input.is_action_pressed("player_back"):
apply_central_impulse(Vector3.BACK)
if Input.is_action_pressed("player_right"):
apply_central_impulse(Vector3.RIGHT)
if Input.is_action_pressed("player_left"):
apply_central_impulse(Vector3.LEFT)
if Input.is_action_pressed("player_jump"):
if jumping == false:
jumping = true
add_central_force(Vector3(0, jumpPower*4, 0))
yield(get_tree().create_timer(1.1), "timeout")
jumping = false
apply_central_impulse(Vector3(0, 0, 0))
func screenPointToRay():
var spaceState = get_world().direct_space_state
var crosshairPos = ui.get_node("Crosshair").rect_position
var rayOrigin = camera.project_ray_origin(crosshairPos)
var rayEnd = rayOrigin + camera.project_ray_normal(crosshairPos) * 2000
var rayArray = spaceState.intersect_ray(rayOrigin, rayEnd)
print(rayArray)
if rayArray.has("collider"):
return rayArray
func _ready():
loadAmmo([5, 50])
updateAmmoUI()