Search Unity

AddForce for object to FixedUPdate on the fly.

Discussion in 'Scripting' started by dansav, Apr 24, 2008.

  1. dansav

    dansav

    Joined:
    Sep 22, 2005
    Posts:
    510
    How do I add more code to the FixedUpdate() routine?

    Say I make a cube using the script and fixedUpdate has nothing in it to start. Then I want to add to FixedUpdate the code
    cube.Rigidbody.AddForce(someVector);
    How do I do that?

    This represents a generalized problem I need to solve. Wherein the user creates or imports objects from the web, names them, then gives them physics. I have no idea how many the user will add ahead of time, what they will name them, or what physics they will give them.

    In the main script...I add a cube when the user clicks a button...

    cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    cube.transform.position = Vector3(0,20,0);
    cube.AddComponent(Rigidbody);



    //for physics
    function FixedUpdate(){

    }

    thanks,

    Dan
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Store an array of Vector3s in the script and then loop through them in the FixedUpdate method. If you don't know how many new forces you will need to add in advance then you might be better using an ArrayList to hold the Vector3s.
     
  3. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Why not always add someVector and change someVector in another function to add various forces by adding together vectors?

    (it is late -- that suggestion may have been worded in a confusing way)