• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Taldoesgarbage · Nov 18, 2020 at 05:09 PM · rotationunity 2dprojectileinstantiate prefab

I need to summon projectiles

I cant figure out how to make the prefab instantiate facing the player, then fire.

     int randomO;
     float randomY;
     float randomX;
     public GameObject projectilePrefab;
     public Transform spawner;
     public Transform player;
     public float WaterSpeed;
 
     private float time;
     public float maxtime;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         randomO = Random.Range(1, 5);
         switch(randomO)
         {
             case 1:
                 randomY = 62.9f;
                 randomX = Random.Range(-83.9f, 83.1f);
                 break;
             case 2:
                 randomY = -66.8f;
                 randomX = Random.Range(-83.9f, 83.1f);
                 break;
             case 3:
                 randomX = -107.9f;
                 randomY = Random.Range(48.7f, -58.7f);
                 break;
             case 4:
                 randomX = 100.6f;
                 randomY = Random.Range(48.7f, -58.7f);
                 break;
         }
 
         transform.position = new Vector2(randomX, randomY);
 
         if (time <= 0.0f)
         {
             //Heres where i want to instantiate the object.
             time = maxtime;
         }
 
     }
 
     private void FixedUpdate()
     {
         if (time >= 0.0f)
         {
             time -= Time.deltaTime;
         }
     }






Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by stephen_gregory · Nov 18, 2020 at 06:59 PM

I didn't test that but I think you can rotate the prefab in the prefab mode then save then should work. Didn't test that but I think it should work.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stephen_gregory · Nov 18, 2020 at 07:05 PM 0
Share

Or if you want to change rotation via script maybe you can use

 public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

And set the rotation , something like this
Instantiate(cubePrefab, Vector3.zero, Quaternion.Euler(45, 0, 0));

avatar image
0

Answer by Omti1990 · Nov 18, 2020 at 10:46 PM

I'd go with something like this:

          if (time <= 0.0f)
          {
              //Heres where i want to instantiate the object.
             GameObject myProjectile = Instantiate(projectilePrefab, spawner.position, spawner.rotation , spawner)
          myProjectile.transform.LookAt(player.position);
 
              time = maxtime;
          }

You could probably also do this by replacing spawner.rotation with player.rotation and multiplying that with some Quaternion to turn the game object the way you want.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

197 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The bullet only moves in one direction and does not rotate with object. Code: 1 Answer

projectiles not correctly aiming in Unity2D 0 Answers

C# 2D Translate Forward Based on Rotation 1 Answer

How do I create a solid GameObject (arrow) made out of multiple child objects (head, shaft, fletching)? 0 Answers

How can I limit objects rotation to fixed range of 0 to 1? 0 being no rotation 1 full rotation? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges