c# - Make character follow mouse using Character Controller -
i have 2d space shooter game (imagine chicken invaders) has been released on ios , android. trying port webgl, , that, i'm trying implement movement using mouse. having had success in implementing movement using unity's built-in character controller on mobile platforms, i'd keep using in webgl build keep code-base small , little platform-specific implementation possible.
therefore, approach should use ship follow mouse cursor?
ps: on mobile used touch.deltaposition charactercontroller.move() have ship follow finger's movement.
edit: current method
input manager
public vector2 getinput () { switch(type) { case platformtype.computer: if (input.getbuttondown("jump") || input.getaxis("fire") != 0f) { thisdelegate.inputdidreceivefirecommand(); } if (!input.mousepresent) { temp.x = input.getaxis("horizontal"); temp.y = input.getaxis("vertical"); } else { if (input.getaxis("mouse x") != 0f || input.getaxis("mouse y") != 0f) { temp.x = input.getaxis("mouse x"); temp.y = input.getaxis("mouse y"); } else { temp.x = input.getaxis("horizontal"); temp.y = input.getaxis("vertical"); } } } playercontroller
void handleinput() { mvm = inputcontroller.getinput(); if (getcomponent<inputmanager>().type == inputmanager.platformtype.mobile) { mvm *= time.deltatime * initialveloc / 10; } else { if (mvm != vector3.zero) { mvm -= transform.position; } } } void update () { charactercontroller.move((vector2)mvm); } current method makes character jitter next center of screen.
i've modified script , i'm taking absolute mouse position instead of delta. here's how looks now:
input manager:
if (input.getaxis("mouse x") != 0f || input.getaxis("mouse y") != 0f) { temp = input.mouseposition; temp.z = 12.5f; // vector3 initialmov = mvm; temp = camera.main.screentoworldpoint(temp); temp -= transform.position; } controller
mvm = inputcontroller.getinput(); mvm *= time.deltatime * initialveloc; charactercontroller.move((vector2)mvm); this method works intended , seems more efficient raycasting method, in terms of cpu usage.
Comments
Post a Comment