- Home /
Instantiating in a specific place with force
Hey guys,
I am writing a quick and easy puzzle game (don't need to go into details). However, it does involve a cannon, which will (obviously) fire something. I'm not very good with instantiation code, and I need to have it fire with force. Note that this is a 2d game, so it will only be going about the Y and X axes. I need the cannon to fire sideways, so the ball needs to be instantiated next to the cannon, and it also needs to have force, but it needs to be a RigidBody.
None of the Unity Documentations on Instantiate tell me what I need to know. Can someone please help me?
Thanks,
-Silent
Answer by Fattie · Jan 02, 2013 at 09:18 AM
basically what you're looking for is this guys ...
var NEWTHING:GameObject;
so NEWTHING will be a game object, ok ?
NEWTHING = Instantiate( yourOtherThing );
instantiate basically "copies" some other thing in your game. It copies every part of it. (It can even be a prefab.) So when you say "it needs a collider" (or whatever) ... obviously just make the original have everything you want .. colliders, sparkles, whatever.
No as soon as you instantiate it, you just immediately set the position, etc it sthat simple
NEWTHING = Instantiate( yourOtherThing );
NEWTHIN.name = "a new name for your thing";
NEWTHING.transform.position = whatever you want;
and so on.
if you (for example) want to add an explosive force t the new item, do so
/Documentation/ScriptReference/Rigidbody.AddExplosionForce.html
NEWTHING.rigidbody.AddExplosionForce( blah blah )
You mention that you want it to sit NEAR some other item in your game, let's say "near your door"
NEWTHING.transform.position = yourDoor.transform.position;
NEWTHING.transform.position += Vector3( 0.50, 0, 0 )
Unity works in meters, so that is half a meter away from the door. OK?
Guys there is awesome beginner articles at unityGEMS.com Good luck !!
Answer by Rabbit · Jan 02, 2013 at 09:13 AM
I am only very new to this and very noobish but i would try finding the game object after I instantiate and then apply the force
gameObject.Find("cannonball").rigidbody.AddForce(transform.forward * 1000)
probably wrong but that is how I would think about it.
good luck.
Your answer
Follow this Question
Related Questions
Add Relative Force 2 Answers
Rotating object and its speed 0 Answers
Move Player Car in Forward Direction using Physics Force 1 Answer
Add force relative to the angle/direction? 1 Answer
Roller Coaster force 1 Answer