Problem with movement script using local and global conversion
I'm working on a movement script for my character controller and I'm having a problem.
In my script, I keep in global var the GLOBAL accumulated speed in a vector3 called globalVelocity. During each of my updates, I:
-turn the controller if necessary
-convert the globalVelocity to local velocity :
Vector3 localVelocity = transform.InverseTransformDirection(globalVelocity);
-increase the speed if the player didn't get to the maxVelocity yet or decrease it if he went over it or he stopped moving (it doesn't immediately cap it, it decrease it over time)
-reconvert it to a global velocity:
globalVelocity = transform.TransformDirection(localVelocity);
The problem is that if the angle between my local velocity and local axis is to small, this happens:
when the global velocity is converted to the local one, for exemple, localVelocity.z > gloabalVelocity.z
than, my code reduce the local Z velocity, because it went over the limit, but again, it just reduce it, so it might still be over the limit. In the end the amount of local Z velocity that is gained from the converison is greater than how much it looses before being reconverted. In the end, I end up with a crazy local velocity!. For exemple, my max is 5, but I have 18.
Those someone might have encounter such a problem before or have a suggestion on an alternative solution for what I'm trying to do?
Your answer
Follow this Question
Related Questions
Character Cannot Move from its Spot 0 Answers
Moving a character with WASD but always face the mouse? 0 Answers
char speed need help 0 Answers
Moving a character with touch controls relative to target? 0 Answers