Search Unity

Ball move by press button on the screen

Discussion in 'Scripting' started by Quast, Jul 28, 2015.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    466
    How to make a a ball move (left or right only) by press a button on the screen. I need a code for this one.

    Thank you
     
  2. GreenBoxInteractive

    GreenBoxInteractive

    Joined:
    Mar 23, 2015
    Posts:
    135
    Look up RididBody.AddForce in the docs.
     
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    466
    I know about this. I made it before with roll a ball.
    The game i work on it now is 3d world but 2d style.
    I want the ball to move at X line (vertical) not Z or Y line.
    I made some codes but didn't work.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,655
    AddForce isn't quite precise enough to be absolutely sure of a given direction on its own, or rather physics in general (even pseudo-physics) is too messy for that on its own. That said, you can still use AddForce to supply the force to drive the object in the given direction easily enough, and precisely, you just have to constrain the Y and Z axis using Rigidbody Constraints, or by simply overwriting the X and Z values of the transform.position for the sphere every frame. It's up to you- people do both pretty commonly. Look at the example on the AddForce page to get an idea of how it works and how to write it.