Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Xanatos_88 · May 05, 2016 at 09:40 PM · instantiateprefabturretgeneric listgetinstanceid

[2D] Set the instantiated projectile rotation from turret using generic list? There are multiple turret prefabs in the scene

Background Info

I have 2 different turret prefabs for placement on the left & right walls of the level. I'm using an animation to make the barrel auto-rotate up and down. When it sees the player through line-cast, it shoots at the player.

For the player, I used GameObject.FindGameObjectWithTag() to get the 2 vector points needed to calculate the correct angle of the projectile. Since there are multiple turret prefab instances in the scene, this isn't going to work.

Code Sample for calculating the angle of player projectile

 // Angle & Direction of projectile
         public GameObject aim_target;  
         private Vector3 aim_pos;   // Vector of aim_target position
         private Vector3 shot_pos;  // vector of shotPoint position
         private float ShootAngle;    // angle for z-axis
         public Vector3 rotateAim;  // vector3 to set rotation angle from ShootAngle
     
         // Spawn Point for projectile
         public GameObject shotPoint;
     
         void Awake()
         {
             shotPoint = GameObject.FindGameObjectWithTag ("spawnpoint");
             aim_target = GameObject.FindGameObjectWithTag ("aim");
         }
     
         void Start()
         {
             aim_pos = aim_target.transform.position;
             shot_pos = shotPoint.transform.position;
             aim_pos.x = aim_pos.x - shot_pos.x;
             aim_pos.y = aim_pos.y - shot_pos.y;
             ShootAngle = Mathf.Atan2 (aim_pos.y, aim_pos.x) * Mathf.Rad2Deg - 0;
     
     //         Set vector for bullet angle
             rotateAim = new Vector3 (0, 0, ShootAngle);
     
             // set rotation of bullet
             transform.rotation = Quaternion.Euler(rotateAim);
         }

My problem is setting the correct rotation for the turret projectile after it has instantiated. I know that generic lists would be good to use for my purposes, but I don't know how to properly apply it.

I need to help on how to do the following:

  1. Create generic list of all turrets in the level

  2. Get the InstanceID for the turret that instantiated that specific projectile.

  3. Use that gameobject reference to execute the code that will calculate and set the proper rotation of the projectile on z axis

Alternatively, I thought about trying to directly apply the turret's rotation to the projectile. I still don't know how to do that correctly in this situation.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xanatos_88 · May 06, 2016 at 01:18 AM

I fixed the issue with the rotation!! I didn't use a generic list though. I placed the code that adjusted the projectile's rotation into the ShootPlayer() function of Turret AI script. The turret AI script is located on the turret barrel object, which is a child of the turret base parent object.

Shoot Player Function

 void ShootPlayer()
     {
         if (targetFound && Time.time > nextFire && !shotDone)
         {
             // Time until next shot
             nextFire = Time.time + fireRate;
             
             int ammoIndex = 0; 
             float shootAngle;
 
             enemyBullet = (GameObject)Instantiate (ammo [ammoIndex], turretShotPoint.transform.position, Quaternion.identity);
 
             Vector2 direction = player.transform.position - transform.position;
             direction.Normalize ();
         
             sight_pos = sightEnd.position;
             shot_pos = turretShotPoint.transform.position;
             sight_pos.x = sight_pos.x - shot_pos.x;
             sight_pos.y = sight_pos.y - shot_pos.y;
             shootAngle = Mathf.Atan2 (sight_pos.y, sight_pos.x) * Mathf.Rad2Deg - 0;
 
             //    Set vector for bullet angle
             rotateLeft = new Vector3 (0, 0, shootAngle);
 
             // set rotation of bullet
             enemyBullet.transform.rotation = Quaternion.Euler(rotateLeft);
 
             if (facingRight) {
                 enemyBullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
             } else if (!facingRight) {
                 //    enemyBullet.rigidbody2D.velocity = new Vector3(Mathf.Cos(shootAngle*Mathf.Deg2Rad),Mathf.Sin(shootAngle*Mathf.Deg2Rad),0)* bulletSpeed;        
                 enemyBullet.transform.LookAt (sightEnd);
                 enemyBullet.transform.rotation = Quaternion.Euler(rotateLeft);
 
                 enemyBullet.GetComponent<Rigidbody2D>().velocity = direction * bulletSpeed;
             }
         }
         shotDone =!shotDone;
     }
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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Create gameobject in other scene... 0 Answers

Trying to get a 2d prefab without rigidbody to instantiate on button press, "the variable T_Bullet has not been assigned." 0 Answers

Can't Instantiate an object as child of another 1 Answer

Cannot convert UnitEngine.Vector3 to UnityEngine.Transform when using .postition 1 Answer

How to instatiate a prefab with all it's characteristics?,Question about how to Instatiate a prefab with all it's characteristics 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges