Search Unity

Doing a kick on a rigidbody

Discussion in 'Scripting' started by oliverdb, Feb 13, 2012.

  1. oliverdb

    oliverdb

    Joined:
    Jan 30, 2010
    Posts:
    165
    I want todo do a kick or its actually a flick since its a flick with the finger on IOS.

    It sounds easy "Just add some force" but I dont want the physics to go wild and everything to bounce everywhere. So basically I should add force and remove it again after some time, what about objects it hits?
     
  2. Norrtec

    Norrtec

    Joined:
    Nov 7, 2011
    Posts:
    10
    The nice thing about force is that it is applied instantly. All you need is a function that's only called once.

    In this example, a forward force is applied to the target rigidbody when the user taps the screen. I'm not familiar enough with touch input to get a flick, though.

    Code (csharp):
    1.  
    2. var kickStrength = 500.0;
    3.  
    4. function Update(){
    5.     for (var i = 0; i < Input.touchCount; ++i) {
    6.         if (Input.GetTouch(i).phase == TouchPhase.Began) {
    7.             Kick();
    8.         }
    9.     }
    10. }
    11.  
    12. function Kick(){
    13.     rigidbody.AddRelativeForce (Vector3.forward * kickStrength));
    14. }
    15.  
     
    Last edited: Feb 14, 2012