Hello Guest

Author Topic: How to instantiate and AddForce to a Sprite?  (Read 8010 times)

RadWayne

  • Newbie
  • *
  • Posts: 1
    • View Profile
How to instantiate and AddForce to a Sprite?
« on: May 27, 2013, 05:25:47 pm »
I'm having trouble effecting Sprites via C# scripts.I have something like this:

Code: [Select]
public transform point;
public tk2dSprite thing;

void Update(){
     tk2dsprite.Instantiate(thing,point.position,point.rotation);
     // Wasn't sure where to go from here


   //Also tried this from Unity's tutorials
   Rigidbody projectile;
   projectile = tk2dsprite.Instantiate(thing,point.position,point.rotation) as Rigidbody;
   projectile.AddForce(point.forward * 1000);
}

Not really sure what I'm doing.  :P

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to instantiate and AddForce to a Sprite?
« Reply #1 on: May 27, 2013, 05:37:17 pm »
You instantiate a sprite the same way you instantiate any object in Unity. Create a prefab, drag and drop a reference and use Instantiate.
Eg. http://docs.unity3d.com/Documentation/ScriptReference/index.Instantiate.html

So once you instantiate a tk2dSprite and have a reference to it, get the rigidbody from it.
instantiatedSprite.rigidbody.AddForce( ... );

The sprite must have a rigidbody for this to work, btw - you must've added it in the prefab.