c# - Make ball Jumping -
i trying make script can move ball, horizontal , vertical. managed working.
but want make ball "jump". ended script below, ball launched rocket xd
can me out
using unityengine; using system.collections; public class playercontroller : monobehaviour { public float speed; public float jumpspeed; public guitext counttext; public guitext wintext; private int count; void start() { count = 0; setcounttext(); wintext.text = " "; } void fixedupdate() { float movehorizontal = input.getaxis ("horizontal"); float movevertical = input.getaxis ("vertical"); vector3 movement = new vector3 (movehorizontal, 0, movevertical); vector3 jump = new vector3 (0, jumpspeed, 0); getcomponent<rigidbody>().addforce (movement * speed * time.deltatime); if (input.getbuttondown ("jump")); getcomponent<rigidbody>().addforce (jump * jumpspeed * time.deltatime); } void ontriggerenter(collider other) { if (other.gameobject.tag == "pickup") { other.gameobject.setactive(false); count = count +1; setcounttext(); } } void setcounttext() { counttext.text = "count: " + count.tostring(); if (count >= 10) { wintext.text = "you win!"; } } }
jumping not work adding continuous force on object. have apply single impulse object once when jump button first pressed. impulse not include time factor, because applied once. this:
bool jumping; if (input.getbuttondown ("jump") && !this.jumping); { getcomponent<rigidbody>().addforce (jumpforce * new vector3(0,1,0)); this.jumping = true; }
also note in example multiplying upward unit vector jumpspeed twice. once in jump
vector initialization , once in addforce
method.
of course, have make sure gravity applies pull object down (and if object hits ground, reset jump bool.
in general, depending on kind of game making, easier set velocity of object , don't work unity physics engine simple moving around.
Comments
Post a Comment