c# - Ball is moving at same velocity despite changing velocity variable in unity -
i creating pong , working fine, however, wanted there power-ups ball velocity changes. have velocity variable seems change nothing speed @ ball starts, velocity stays flat. here image of ball panel.
and here code inside ball script
public float ballvelocity = 1000; rigidbody rb; bool isplay; int randint; public vector3 velocity; float elapsed = 0; private bool onleft; public vector3 startpos = new vector3(0, 0, 0); void awake() { rb = getcomponent<rigidbody>(); randint = random.range(1,3); velocity = new vector3 (ballvelocity, ballvelocity, 0); } void update() { if (rb.transform.position.x < gameobject.findgameobjectwithtag ("paddle").transform.position.x) { print ("game over"); } if (rb.position.x < 0) onleft = true; else onleft = false; elapsed += time.deltatime; rb.drag = 0; print(onleft); if (elapsed > 5) { //rb.velocity *= .5f; elapsed = 0; } rb.maxdepenetrationvelocity = ballvelocity; if (input.getmousebutton(0) && isplay == false) { transform.parent = null; isplay = true; rb.iskinematic = false; if (randint == 1) { rb.addforce(new vector3(ballvelocity, ballvelocity, 0)); } if (randint == 2) { rb.addforce(new vector3(-ballvelocity, -ballvelocity, 0)); } } }
}
no matter have tried, or have looked up, nothing seems help. please me this. time!
the simplest way change object's speed grab it's current velocity rb.velocity, normalize .normalized, , set rb.velocity vector multiplied speed @ want ball go at.
here example code doing this:
void changespeed(float speed) { vector3 dir = rb.velocity.normalized; rb.velocity = dir * speed; }
if system lossless (100% bounce, no drag, on) after doing this, ball stay @ speed until change again.
Comments
Post a Comment