unity3d - Messing up Vectors in C# (Unity Engine) -
i'm new programming in c# in unity , there problems when moving in z-axis. problem continue move when let go of button. however, when move in x-axis fine, letting go of button stop player. code below:
using unityengine; using system.collections; public class player : monobehaviour { public vector3 motion = new vector3(); private charactercontroller controller; private bool onground; private float xrot, yrot; public static float x_rotation = 0; public static float y_rotation = 0; private const float lookspeed = 2.0f; public void start() { controller = getcomponent<charactercontroller>(); } public void fixedupdate() { if(screen.lockcursor) { vector3 impulse = new vector3(); if(input.getkey(keycode.w)) impulse.z+=1; if(input.getkey(keycode.a)) impulse.x-=1; if(input.getkey(keycode.s)) impulse.z-=1; if(input.getkey(keycode.d)) impulse.x+=1; if(impulse.sqrmagnitude > 0) { motion += quaternion.euler(new vector3(0, xrot, 0)) * impulse.normalized * 0.05f; } if(onground) { if(input.getkey(keycode.space)) { motion.y += 0.2f; } } motion.y -= 0.015f; vector3 oldmotion = motion; vector3 oldpos = controller.transform.localposition; controller.move(motion); vector3 newpos = controller.transform.localposition; motion = newpos - oldpos; onground = oldmotion.y < -0.0001f && motion.y >= -0.0001f; motion.x *= 0.8f; motion.y *= 0.8f; } } public void update() { if(screen.lockcursor && input.getkeydown(keycode.escape)) { screen.lockcursor = false; } if(!screen.lockcursor && input.getmousebuttondown(0)) { screen.lockcursor = true; } if(screen.lockcursor) { xrot += input.getaxis("mouse x") * lookspeed; yrot -= input.getaxis("mouse y") * lookspeed; if(yrot < -90) yrot = -90; if(yrot > 90) yrot = 90; if(xrot < -180) xrot += 360; if(xrot >= 180) xrot -= 360; controller.transform.localrotation = quaternion.euler(new vector3(yrot, xrot, 0)); } player.x_rotation = xrot; }
}
the last few statments in fixedupdate() have coded-
motion.x *= 0.8f; motion.y *= 0.8f;
should contain
motion.z *= 0.8f;
and may u dont need-
motion.y *= 0.8f;
Comments
Post a Comment