2D Toolkit Forum

2D Toolkit => Support => Topic started by: RadWayne on May 27, 2013, 05:25:47 pm

Title: How to instantiate and AddForce to a Sprite?
Post by: RadWayne 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
Title: Re: How to instantiate and AddForce to a Sprite?
Post by: unikronsoftware 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.