- Home /
VR Sword tracking
Hi, I am currently making the functions of a VR game, but I am a bit stuck at the sword tracking. I want the game to apply damage to enemies by two factors: Velocity and Direction. So if you stab or hit an enemy with the sharp edge of the sword, it takes damage, that is multiplied by the speed of the swing.
Right now, velocity is working fine, but the direction tracking is lacking. I want to track the stab velocity by calculating how far the sword have moved in it's local Y direction, from last frame. And this is a VR game, so you are able to stab in any direction.
I also want to track the swing. The sharp edge of the blade is pointing in the sword's Z direction, so I again need to calculate how far the sword have moved in it's local Z direction, acording to the sword's local Z direction last frame. That means if you hit the enemy with the wide side, or the dull side, of the sword (it's X direction) less damage is applied.
I hope some smart Vector people can help me out :)
Answer by hexagonius · Feb 22, 2019 at 04:32 PM
Dot product to the rescue (https://docs.unity3d.com/ScriptReference/Vector3.Dot.html):
It can be used in two ways basically: it can return the percentage of how much two vectors point into the same direction, or it can give the portion of a vector in relation to the direction of another vector. What I would do is use the Dot product for the latter. Use it with the normalized velocity and the world direction of the swords y direction. as long as that number is larger than .707, the sword is pointing into velocity direction, 45 degrees off at max.
The same works for the velocity and the swords world z direction. The dot will return the amount of velocity the sword is doing in the right direction. You could just accumulate them until the velocity is too slow, or the opposite of the last movement, or at least to some degree (again, Dot product with current to last velocity, but normalized)