I want to add a vector3 to velocity, but it will keep adding them together.
If the boost has a length of 2, and velocity has 10, I want it to equal 12. Not 12, 14, 16, 18, etc.
You need to find out where you have the bug that keeps adding the vectors together.
If I the boost to velocity every physics frame, it will keep adding upon itself.
So, its in the physics process.
That seems like a good place to look.
So, what you can do is set up your boost condition if boost: velocity = velocity + boost_spd, boost = false.
Then add in some code to reset velocity once boost is finished. You need to put a guard on your boost function to prevent it from continuing to be iterated over in physics, it is easy to do with a boolean var.
Yep, you completely right. But that’s not exactly what I’m looking for. I’m not sure how else to explain it but imagine the boost is 2, and is slowly moved toward 0. So, over time it loses power.
Plus I want it to be perpetually updated because I wanted it to be used for velocity redirection.
The cost of being perpetually updated is being perpetually added, which is my probablem.
I also don’t want there to be a cap because the velocity might increase higher than 10 sometimes…
Tbh, i’m starting to think it’s impossible, but that’s not usually the case when it comes to math.
It’s fairly easy at its core:
- Have a unit vector that points in the boost direction.
- Have a boost speed by which you multiply the unit vector each frame.
- Have a bleed speed by which the boost speed decrements each frame until it reaches zero.
- Each frame, multiply the boost direction unit vector by the boost speed, then add that to your current velocity vector.
So, what Soap said should work.
You should be able to create a couple var:
boost_force : int = 2
current_boost : Vector3
boosting : bool
boost_decay : float
boost_time : Vector3
I don’t know how you handle boost commands, I am assuming via a key input:
Input:
Here you would get input direction for boost, and set current_boost = boost_force * boost direction. You would then set boost_time = current_boost * boost_decay, and boosting = true.
if input.is_action_pressed(boost):
var input_dir = #Get input direction here
current_boost = boost_force * input_dir
boost_time = current_boost * boost_decay
boosting = true
Physics:
In physics you would control your boost.
if boosting:
velocity += boost_current
boosting = false
if not boost current == 0:
boost current -= boost_time
velocity -= boost_time
If you want to change direction when you boost you will need to also change your current velocity.
if this is the case you could add a change_velocity var.
if boosting:
current_velocity = velocity.x + velocity.y
velocity += boost_current + (current_velocity * boost_current.normalized())
#I haven't tested this, so I am not sure if this would do what I think it should
boosting = false
if not boost_current == 0:
boost_current -= boost_time
velocity -= boost_time
The idea is to multiply the current velocity by the normalized boost_current to get direction but with the current velocity accounted for, so that way you can just add the boost on top, this will though completely and instantly change your direction fully, if that is what you are looking for.