unity3d - Basic C# Unity2D movement script not functioning -
hi there fellow overflowers! have dived c# in unity, because believe has more functionality unityscript, , come c++ background. referencing code 2d script in sample assets, have tried create own. code below:
using unityengine; public class playercontrol : monobehaviour { //variables [hideininspector] public bool facingforward = true; //for flip() function bool isground = true; //for grounded() function public float maxspeed = 5.0f; //terminal sideways velocity public float horizonaxis; //checks a/d movement public float jumpfloat = 1000.0f; //modular, use unity editor public float movefloat = 400.0f; // " " void start() { //transform.position(0,0,0); } void flip() { facingforward = !facingforward; //switches boolean vector3 thescale = transform.localscale; //assigns vector localscale of player thescale.x *= -1; //if x = 1, position becomes -1 , flips transform.localscale = thescale; //reassigns localscale update thescale } bool grounded() { if (transform.position.y > 1) { //if position of gameobject greater 1, not grounded isground = false; } else { isground = true; } return isground; //function returns true or false isground } void update() { horizonaxis = /*unityengine.*/input.getaxis ("horizontal"); //assigns horizonaxis a/d movement unityengine.input if (horizonaxis * rigidbody2d.velocity.x > maxspeed) { // if input a/d current x velocity of gameobject greater maxspeed rigidbody2d.velocity = new vector2 (mathf.sign (rigidbody2d.velocity.x) * maxspeed, rigidbody2d.velocity.y); //1 or -1 times max speed, depending on direction } else if (horizonaxis * rigidbody2d.velocity.x < maxspeed) { //if input a/d less terminal velocity rigidbody2d.addforce(vector2.right * horizonaxis * movefloat); //add force right equivilant input scalar movefloat } if (input.getbuttondown ("jump")) { //if space if(isground) { //and isground returns true rigidbody2d.addforce(new vector2(0.0f, jumpfloat)); //add upwards force bottom of rigidbody2d isground = false; //resets isground value } } if (horizonaxis > 0 && !facingforward) {//if unityengine.input right , facing left flip (); //execute } else if (horizonaxis < 0 && facingforward) { //else flip (); //execute } } }
unfortunately, code doesn't work. no compile errors, input not effect current position of character. should using transform.translate change position, or stick addforce vector2 until character hits maxspeed?
thanks heaps :)
i (kinda) fixed jerky jumping issue, input in update() function switches bool in fixedupdate()
void update () { if (input.getkeydown (keycode.space) && playergrounded) { playerjumped = true; } }
then in fixedupdate() looked bool , did addforce
void fixedupdate () { if (playerjumped) { playerjumped = false; rigidbody2d.addforce (new vector2 (0, jumpforce),forcemode2d.impulse); playergrounded = false; } }
setting playerjumped false makes sure doesn't run several times , player can't jump again because set grounded false. grounded gets set true when player collides things tagged "ground".
i'm still new unity , c# overall can't guarantee best (or good) way. hope somehow though ;)
Comments
Post a Comment