i have character in unity use move script, transform position.x, y, or z everytime when press appropriate button. change code rotate character when user press "a" or "d" , don't move it, if user press "w" button move forward. have looked on internet, , found out need use vector3.forward somehow, doesn't work.
here script:
this script moves character: (it attached player object contains moveable characters)
public class move : monobehaviour { float lerptime; float currentlerptime; float perc = 1; vector3 startpos; vector3 endpos; bool firstinput; public bool justjump; // update called once per frame void update () { if (input.getbuttondown("up") || input.getbuttondown("down") || input.getbuttondown("left") || input.getbuttondown("right")) { if (perc == 1) { lerptime = 1; currentlerptime = 0; firstinput = true; justjump = true; } } startpos = gameobject.transform.position; if (input.getbuttondown("right") && gameobject.transform.position == endpos) { endpos = new vector3(transform.position.x + 0.5f, transform.position.y, transform.position.z); } if (input.getbuttondown("left") && gameobject.transform.position == endpos) { endpos = new vector3(transform.position.x - 0.5f, transform.position.y, transform.position.z); } if (input.getbuttondown("up") && gameobject.transform.position == endpos) { endpos = new vector3(transform.position.x, transform.position.y, transform.position.z + 0.5f); } if (input.getbuttondown("down") && gameobject.transform.position == endpos) { endpos = new vector3(transform.position.x, transform.position.y, transform.position.z - 0.5f); } if (firstinput == true) { currentlerptime += time.deltatime * 5; perc = currentlerptime / lerptime; gameobject.transform.position = vector3.lerp(startpos, endpos, perc); if (perc > 0.8f) { perc = 1; } if (mathf.round(perc) == 1) { justjump = false; } } } }
and here "rotate script", attached moveable character(s) itself:
public class animationcontroller : monobehaviour { animator anim; public gameobject theplayer; // use initialization void start () { anim = gameobject.getcomponent<animator>(); } // update called once per frame void update () { move movescript = theplayer.getcomponent<move>(); if (movescript.justjump == true) { anim.setbool("jump", true); } else { anim.setbool("jump", false); } if (input.getbuttondown("right")) { gameobject.transform.rotation = quaternion.euler(0, 90, 0); } if (input.getbuttondown("left")) { gameobject.transform.rotation = quaternion.euler(0, -90, 0); } if (input.getbuttondown("up")) { gameobject.transform.rotation = quaternion.euler(0, 0, 0); } if (input.getbuttondown("down")) { gameobject.transform.rotation = quaternion.euler(0, 180, 0); } } }
could give me please on this? (preferably code, if can :) )
assuming want character move facing, , trying not lot of changes code, done:
first, attach both script directly movable character.
then, change move script to:
public class move : monobehaviour { float lerptime; float currentlerptime; float perc = 1; vector3 startpos; vector3 endpos; bool firstinput; public bool justjump; // update called once per frame void update () { if (input.getbuttondown("up") || input.getbuttondown("down") || input.getbuttondown("left") || input.getbuttondown("right")) { if (perc == 1) { lerptime = 1; currentlerptime = 0; firstinput = true; justjump = true; } } startpos = gameobject.transform.position; if (input.getbuttondown("up") && gameobject.transform.position == endpos) { endpos = transform.position + gameobject.transform.rotation * (new vector3(0, 0, 0.5f)); } if (input.getbuttondown("down") && gameobject.transform.position == endpos) { endpos = transform.position + gameobject.transform.rotation * (new vector3(0, 0, 0.5f)); } if (firstinput == true) { currentlerptime += time.deltatime * 5; perc = currentlerptime / lerptime; gameobject.transform.position = vector3.lerp(startpos, endpos, perc); if (perc > 0.8f) { perc = 1; } if (mathf.round(perc) == 1) { justjump = false; } } } }
and animationcontroller script to
public class animationcontroller : monobehaviour { animator anim; public gameobject theplayer; // use initialization void start () { anim = gameobject.getcomponent<animator>(); } // update called once per frame void update () { move movescript = theplayer.getcomponent<move>(); if (movescript.justjump == true) { anim.setbool("jump", true); } else { anim.setbool("jump", false); } if (input.getbuttondown("right")) { gameobject.transform.rotation = gameobject.transform.rotation * quaternion.euler(0, 90, 0); } if (input.getbuttondown("left")) { gameobject.transform.rotation = gameobject.transform.rotation * quaternion.euler(0, 90, 0); } } }
this make character rotate when press left , right buttons, , move when press , down button. althought solves problem, not best way write scripts character movement.
i recommend read more state machine design patter. book robert nystrom has chapter , easy read. also, free read online :)
Comments
Post a Comment