- Home /
How Do You To Make An Object Shoot A Projectile?
How DO You Make An Object Shoot A Projectile In Unity So That The Projectile Goes Straight And Only Last A Certain Amount Of Time Before It Disappears. Also What Object Would I Put This Script In.
Answer by Cherno · Jun 26, 2015 at 02:26 AM
To create the projectile, you use the Instantiate function to make a copy of a GamneObject (commonly a Prefab). To make it face the right direction, you set it's transform.rotation to the rotation of the object that "shoots" it. This can be passed directly as a parameter of the Instantiate function. To Destroy it after a certain amount of time, you use the Destroy() function:
Destroy(yourProjectileGameObject, lifetimeInSeconds);
To make it move, you have multiple options depending on your game. You can use AddForce with a RigidBody, or Transform.Translate, or Transform.MoveTowards, or Vector3.Lerp...
The script can be on the object that shoots the projectile, or the character that uses it. Theoretically, it can be anywhere as long as you have the required variables available (the script needs to know where to instantiate, for example).
Your answer

Follow this Question
Related Questions
How to avoid speed change in bullets while moving? 1 Answer
Raycast2D enemy and player problem 0 Answers
How can i fix this shooting code in a 2D plane? 0 Answers
Projectile move towards mouse cursor 3 Answers
Projectile Wont fire 2 Answers