![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | SharkPetro |
I have a function _CharacterControl and _ApplyGravity that are called every frame being:
func _process(delta):
_CharacterControl(delta, _GetInputAxis())
func _CharacterControl(delta, inputvector):
var velocity = Vector3()
velocity -= _ApplyGravity(delta, gravity)
velocity = move_and_slide(velocity, Vector3.UP, true, 1, deg2rad(45), false)
func _ApplyGravity(delta, gravity):
var gr_add = 0
if !is_on_floor():
gr_add = gravity
else:
gr_add = 0.1
return Vector3.UP * gr_add * delta
The character just floats in the air.
It’s made that way because I wanted to not initialise the variables globally, just inside the functions where they are used, no matter what I do I can’t get this code to work, I have however already done it the other way being:
var velocity = Vector3()
func _process(delta):
_CharacterControl(delta, _GetInputAxis())
func _CharacterControl(delta, inputvector):
_ApplyGravity(delta, gravity)
velocity = move_and_slide(velocity, Vector3.UP, true, 1, deg2rad(45), false)
func _ApplyGravity(delta, gravity):
var gr_add = 0
if !is_on_floor():
gr_add = gravity
else:
gr_add = 0.1
velocity -= Vector3.UP * gr_add * delta
I have not given any other pieces of code because they work fine and the problem remains without them. Every other thing that I tried failed in the same way so this chunk of code and the output method are surely the things that are responsible.
The only way this works is when I change the velocity vector inside of the gravity function but it doesn’t work if I calculate only the velocity delta inside of it and then add it to the velocity vector outside wich is exactly what I need to do as I can’t use local variables of another function without using them as arguments for a function, I really don’t want to litter the list of global variables with ones that are only used in one function, that’s why I’m even doing this whole thing, it doesn’t matter where I create the variable though, it still doesn’t work with the first approach and works with the second.
I can not get the first variant to work while the second works flawlessly despite them being absolutely equivalent to each other. I tried many different ways to do this, print(_ApplyGravity(delta, gravity))
in the first case and print(Vector3.UP * gr_add * delta)
in the second give the same result, is_on_floor
is working correctly, all the values match up when using print()
yet the character moves perfectly with the second approach and almost doesn’t move with the first, the velocity doesn’t exceed gravity * delta
, I have been sitting there for hours changing the order of operations, rewriting everything from scratch more than 10 times, initialising variables in different places, nothing works, I tried just copying the working piece of code and changing the output method to the one needed and not changing the way it works basically completely killing the whole purpose of this function and it still didn’t work because of the piece of it that shouldn’t even matter, I’ve given up on trying to make sense out of it, I beg someone to please tell me what even is the difference there and how in hell are those two ways of doing this not equivalent to each other. I’m pulling my hair out and I know that in the end it will be because of something so stupid and simple and that I just didn’t know about.
I’m sorry for my english and for that this whole question is so messy, I hope it isn’t much of a bother, I’m just so tired and frustrated, it might have even just been solved by a break and a bit of sleep but I’m too angry to go to sleep without doing everything I could.
edit:
It turned out to be simpler but at the same time more confusing/
this works:
func _process(delta)
_CharacterControl(delta, inputvector, velocity)
func _CharacterControl(delta, inputvector, velocity):
_ApplyGravity(delta, gravity)
func _ApplyGravity(delta, gravity):
velocity -= Vector3.UP * 9.8* delta
and this doesn’t:
func _process(delta)
_CharacterControl(delta, inputvector, velocity)
func _CharacterControl(delta, inputvector, velocity):
velocity -= Vector3.DOWN * 9.8 * delta
that’s it, my computer is haunted, I quit gamedev and any programming in general for good if this doesn’t have an explanation, this is just ridiculous
anoher edit:
yes, it’s even worse, I just tried and it turns out that everything works fine when I call the gravity function in _process
, but it still doesn’t work when I call it inside of _CharacterControl
just as I described previously, and _CharacterControl
is in _process
too, there is literally nothing going on other than functions being called inside of other functions, I removed the rest of the code and all the other scripts in the scene, this is just _process
that contains _CharacterControl
that contains _ApplyGravity
, and somehow it gives different results depending on where I change the velocity vector, I can’t even see the pattern there