- Home /
Instantiate projectile at position of specific game object
I've built a button that charges and fires an arrow, however with the code i'm using currently, the arrow instantiates at the button itself. How do I make the arrow spawn at position of a specific game object (the bow)?
Here's the code:
public void ReleaseButton()
{
GameObject arrow = Instantiate(arrowPrefab, transform.position, Quaternion.Euler(new
Vector3(0, 0, 90))) as GameObject;
arrow.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
Thank you for your time
Answer by highpockets · Aug 17, 2020 at 10:30 PM
Well in your instantiate code, it shows you are having the position transform.position
and that will give it the position of the game object that the script is attached to. if you want to give it a different position, pass that position to the instantiate function.
[SerializeField] Transform transformToSpawnArrowAt;
public void ReleaseButton()
{
GameObject arrow = Instantiate(arrowPrefab, transformToSpawnArrowAt.position, Quaternion.Euler(new
Vector3(0, 0, 90))) as GameObject;
}
Thank you kindly for your help that solves it! You're the best.
Your answer
Follow this Question
Related Questions
Instantiating objects at different locations 0 Answers
Track and change button y position 0 Answers
How set position of spawning clones 1 Answer
New GUI and Inventory problem. 1 Answer