- Home /
Diagonal Movement with different Speed each direction
Hi,
i searched this topic via google but did not find anything about it. Every thread solves the problem of too fast diagonal movement with using the direction normal multiplied by speed.
My problem is, that i want to influence the strafe speed. Also my character shall walk forward faster than backward (like in reality). So i have different speeds each direction.
Vector3 vx2 = new Vector3(transform.right.x * cmx.value, 0, transform.right.z * cmx.value);
Vector3 vz2 = new Vector3(transform.forward.x * cmz.value, 0, transform.forward.z * cmz.value);
Vector3 result = vx2 + vz2;
cmx.value and cmz.value are the speed values. On x-axis i can move max 2 and on z-axis i have a max of 5. If i move diagonal i got 5.3.
I think multiplying with normal makes sense, but the addition may not correct. I thought about Vector3.Cross but this resulted in strang low values...
Can you help me please?
Answer by Bunny83 · Apr 05 at 07:18 AM
Well, first of all normalizing the input direction vector is not really a good idea. You're loosing any ease in / ease out since you always jump to full throttle. Using ClampMagnitude is usually the best solution.
About different movement speed in different directions, you can always queue that after the clamping. So first create your clamped direction vector that does not go above 1 in length. However after that you can simply split the two axis again. Keep in mind that means you can get all sorts of mixed max velocities depending on the angle.
I would recommend to stick in local space for all those calculations. That way it's much easier to split the velocity into its local components. At the end you can simply use transform.TransformDirection to get the worldspace velocity.
Answer by Tornby · Apr 05 at 10:52 AM
After further research in community i finally found:
https://answers.unity.com/questions/1736157/clampmagnitude-but-with-different-speeds.html
This is exactly what i was looking for. The solution is pretty smart by using an animation curve for relation between angle and the movement speed.