How to give Vector3 position to object being instantiated
So I want to instantiate an object (bullet) that will blow up at where it was clicked (flak cannon).
This is what I use for Instantiate
Instantiate(bulletPrefab, firePointOne.position, firePointOne.rotation)
This is a function for getting mouse position (located in update)
//mouse position to trigger bullet
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
and inside the bullet's script would be this variable that needs to receive the value
public Vector3 blowCation;
Then I just need to have it blow up on that position, but I can do that myself, I just need to get the position for the bullet.
Answer by Reid_Taylor · Aug 30, 2020 at 06:21 AM
Because you have public Vector3 blowCation
I am assuming that you have a script on the bullet... So I would take the bulletPrefab in Instantiate(bulletPrefab, firePointOne.position, firePointOne.rotation)
and instead of referencing a GameObject use the bullet script. So something like this: not tested:
public Bullet bulletPrefab;
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Bullet bullet = Instantiate(bulletPrefab, firePointOne.position, firePointOne.rotation);
bullet.blowCation = mousePosition;
Hope this helps! lemme know if this doesn't work...
Your answer
Follow this Question
Related Questions
Access to public variable of instantinated prefab not working 1 Answer
Why cant I access a PUBLIC function (Cs, VS2015 and 2D)? Help? 1 Answer
public or private, but what about no class before a variable? 3 Answers
Problem with decrement 0 Answers
I need to activate a function in another script from a script on a different game object 0 Answers