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 /
avatar image
0
Question by WyrmrestGames · Nov 30, 2018 at 07:47 PM · transform.rotationgameobject.tagturrets

2D Rotation to target?

Hey guys, hopefully, you can help me find an answer to what I'm doing wrong. I'm trying to get this turret to automatically reset to new targets and transform its rotation to a new target each time one goes out of range or is destroyed.

So far this is what I've got, I haven't set it up to actually fire bullets yet, but the Rotation works, its just not selecting new and alive targets.

Please note that this is written in C# and not JavaScript...

Thanks in advance to anyone that can help!

Code:

     bool isInRange;
 
     GameObject target;
 
     bool hasTarget;
 
     float rotationSpeed = 6.0f;
 
     Rigidbody2D turretRigid;
     CircleCollider2D turretCollider;
 
     EnemyStats enemyStats;
     GameController gameController;
     WaveSpawner waveSpawner;
 
     private void Awake()
     {
         gameController = GameObject.Find("GameController").GetComponent<GameController>();
         waveSpawner = GameObject.Find("WaveSpawner").GetComponent<WaveSpawner>();
     }
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(isInRange == true && hasTarget == true && )
         {
             Invoke("AquireTarget", 0);
             transform.Rotate(new Vector3(target.transform.position.x, 0 , -rotationSpeed) * Time.deltaTime);
         } else if(isInRange == true && hasTarget == false)
         {
             CancelInvoke("AquireTarget");
         }
         
     }
 
     void AquireTarget()
     {
         if(hasTarget == true)
         {
             target = GameObject.FindWithTag("Enemy");
         }
     }
 
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if(collision.gameObject.tag == "Enemy")
         {
             hasTarget = true;
             isInRange = true;
         }
     }

Comment
Add comment · Show 1
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 WyrmrestGames · Nov 30, 2018 at 08:38 PM 0
Share

Update on the code: bool isInRange;

     float range;
 
     public GameObject turretShell;
 
     public GameObject target;
 
     public GameObject theTurret;
 
     bool hasTarget;
 
     Vector2 spawnPosition;
 
     float rotationSpeed = 200.0f;
 
     Rigidbody2D turretRigid;
     CircleCollider2D turretCollider;
 
     EnemyStats enemyStats;
     GameController gameController;
     WaveSpawner waveSpawner;
 
     private void Awake()
     {
         gameController = GameObject.Find("GameController").GetComponent<GameController>();
         waveSpawner = GameObject.Find("WaveSpawner").GetComponent<WaveSpawner>();
         theTurret = GameObject.Find("HeavyTurret");
     }
 
     // Update is called once per frame
     void Update () {
 
         spawnPosition = new Vector3(theTurret.transform.position.x, theTurret.transform.position.y, theTurret.transform.rotation.z + rotationSpeed);
 
         if (waveSpawner.isSpawned == true)
         {
             enemyStats = GameObject.FindWithTag("Enemy").GetComponent<EnemyStats>();
         }
 
         if(isInRange == true && hasTarget == true && waveSpawner.isSpawned == true)
         {
             transform.Rotate(new Vector3(target.transform.position.x, target.transform.position.y, -rotationSpeed) * Time.deltaTime);
         }
 
         if(isInRange == true && hasTarget == true && waveSpawner.isSpawned == true)
         {
             StartCoroutine(StartFiring());
         } else if(isInRange == false && hasTarget == false)
         {
             StopCoroutine(StartFiring());
         }
         
     }
 
     IEnumerator StartFiring()
     {
         while(hasTarget && isInRange == true)
         {
             yield return new WaitForSeconds(0.5f);
             Instantiate(turretShell, spawnPosition, Quaternion.identity);
         }
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if(collision.gameObject.tag == "Enemy")
         {
             target = GameObject.FindWithTag("Enemy");
             hasTarget = true;
             isInRange = true;
         }
     }
 
     private void OnTriggerExit2D(Collider2D collision)
     {
         if (collision.gameObject.tag == "Enemy")
         {
             hasTarget = false;
             isInRange = false;
         }
     }

I got it to rotate and sort of change targets but the bullets don't transform at the same rotation as the turret.

0 Replies

· Add your reply
  • Sort: 

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

95 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

Related Questions

Array elements change Gameobject.tag 1 Answer

How to change the pivot point of a mesh in script? 1 Answer

Instantiate facing world zero on Y axis? 0 Answers

Why is my object rotation going back before rotating 1 Answer

How to make a bone rotate facing towards a given Transform? 0 Answers


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