How can I fix this? (3D Movement Issues)
Asked Answered
T

3

0

Hey new here. I am having a problem with 3D movement. I want a bool (triggered by an input) to change the character speed but also change it's speed to something else if the button is pressed and the speed value is equal to or above 0.1. Here is my code:

if Input.is_action_pressed("box_form"):
		is_in_box_form = true
	else:
		is_in_box_form = false
	
	if is_in_box_form:
		speed = box_form_speed
	elif is_in_box_form && speed >= 0.1:
		speed += box_charge_speed
	else:
		speed = 7.0

(btw 7.0 is the normal speed value)

With this code the "box form" does work in the first if statement but "box charge speed" doesn't affect anything in the elif statement. Any help with this?

Timberhead answered 16/1, 2023 at 21:28 Comment(0)
E
0

Did you mean?

if Input.is_action_pressed("box_form"):
	is_in_box_form = true
else:
	is_in_box_form = false

if is_in_box_form:
	speed = box_form_speed

if is_in_box_form && speed >= 0.1:
	speed += box_charge_speed
else:
	speed = 7.0
Exhibition answered 17/1, 2023 at 16:32 Comment(0)
T
0

Exhibition Ah thank you, I thought it would work if I put an 'else if' statement. I am having an issue still though; if I am at a standstill and then press the 'box_form' button and move, it'll still use the charge speed rather than the 'box_form_speed'. I tried fixing that by writing:

if is_in_box_form && speed == 0:
      speed = box_form_speed

I think the issue is that the speed value isn't determined by the button pressure (or stick pressure) like in most games. I think figuring out how to make a current speed that is affected by that would fix my issue. Thanks for the step in the right direction 🙂

Timberhead answered 21/1, 2023 at 20:46 Comment(0)
E
0

Timberhead
np. hmmm I think you could add a counter to store number of milliseconds when button is pressed, so you could make movement value/displacement portional to that ms value, to minic button presss 'pressure' when pressed-down.

Some pesudo code:

@onready var pressed_seconds = -1
const var FACTOR = 1000

if Input.is_action_pressed("box_form"):
	is_in_box_form = true
        pressed_seconds > 0:
          pressed_seconds += time_elapsed(the function name varies between 3.x and 4.x, can't recall the name exactly -.-)
        else:
          pressed_seconds = 0
else:
	is_in_box_form = false
        pressed_seconds = --1

if is_in_box_form:
	speed = box_form_speed

if is_in_box_form && speed >= 0.1:
	speed += box_charge_speed * (pressed_seconds / FACTOR)
else:
	speed = 7.0
Exhibition answered 23/1, 2023 at 12:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.