c# - My sword is swinging not the way I want it to go? -
i added sword swinging script sword, , works fine. want sword go forwards. goes side side, , random ways when turn. there way can fix this? thank you.
using unityengine; using system.collections; public class sword : monobehaviour { private float swingduration = 0.5f; private float swingspeed = 0.22f; private float swingtimer = 0f; private bool swinging = false; private vector3 startrot; void start () { startrot = transform.eulerangles; } // update called once per frame void update () { if (input.getmousebuttondown(0) && !swinging) { swinging = true; } if (swinging) { swingtimer += time.deltatime; if (swingtimer < (swingduration / 2)) { transform.eulerangles = vector3.lerp(startrot, new vector3(0, 0, 1), swingspeed); } if (swingtimer > (swingduration / 2)) { transform.eulerangles = vector3.lerp(transform.eulerangles, startrot, swingspeed); } if (swingtimer > swingduration) { swingtimer = 0f; swinging = false; } } } }
vector3.lerp has signature:
public static function lerp(from: vector3, to: vector3, t: float): vector3; where t within [0..1], t=0 from t=0 to ad result. i.e.the "swing" effect comes third argument constant in case. reasonable argument
swingtimer *2 / swingduration or (for "back swing")
(swingtimer-swingduration/2) *2 / swingduration another problem change fromangle (1 argument) instead of 3rd argument:
transform.eulerangles = vector3.lerp(transform.eulerangles, startrot, swingspeed); this causes "random" effect, experienced.
Comments
Post a Comment