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 AevinGames · Sep 13, 2015 at 06:12 PM · c#2dinstantiate

Making a 2D sprite look at another object when instantiated...

Hey, Im making a 2D tower defense game, I made a script that's supposedly meant to make the turret look at the incoming enemy. The problem I have is that it wont look when the enemy has instantiated.

Although when I manually drag & drop the enemy sprite into the inspector slot the turret turns to face the enemy sprite...

No script ive found fixed my problem so please help.

  using UnityEngine;
  using System.Collections;
  
  public class AISmoothLookAT : MonoBehaviour {
      
      public Transform enemy;
      GameObject TheTarget;
      public GameObject gunTurret;
  
      void Awake () {
          TheTarget = enemy.gameObject;
      }
  
      void Update() {
          OnRotate ();
      }
  
      void OnRotate() {
  
          Vector3 dir = TheTarget.transform.position - this.transform.position;
          float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
  
          gunTurret.transform.rotation = Quaternion.Euler(0, 0, angle);
      }
  
  
  }
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 AevinGames · Sep 14, 2015 at 08:06 PM 0
Share

Any answers yet???

1 Reply

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

Answer by AevinGames · Sep 16, 2015 at 08:53 PM

Solved my own problem, but the script needs to be in an empty gameobject. The console will also spam "TheTarget of AISmoothLookAt" has not been assigned every time you left click. Ignore that.

 using UnityEngine;
 using System.Collections;
 
 public class AISmoothLookAT : MonoBehaviour {
     
     public Transform TheTarget;
     public Transform TheTurret;
     public GameObject TurretOB;
     public GameObject enemyOB;
     private Transform currentBuilding;
     private bool hasPlaced;
     public Camera Camy;
 
     void Awake () {
     }
 
     void Update() {
         OnRotate ();
 
         if (currentBuilding != null && !hasPlaced) {
             Vector3 m = Input.mousePosition;
             Vector3 p= camera.ScreenToWorldPoint(m);
             currentBuilding.position = new Vector3(p.x,p.y,0);
             
             if (Input.GetMouseButtonDown(0))  {
                 hasPlaced = true;
                 Camy.enabled = true;
             }
         }
     }
 
     void OnRotate() {
         if (TheTarget == null) {
                         return;
 
                 }
         else{
                         Vector3 dir = TheTarget.transform.position - TheTurret.transform.position;
                         float angle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg;
 
                         TheTurret.transform.rotation = Quaternion.Euler (0, 0, angle);
                 }
     }
 
     public void Turretbuy () {
         
         hasPlaced = false;            
         currentBuilding = TheTurret= ((GameObject)Instantiate (TurretOB)).transform;
         WoodManager.Woody = WoodManager.Woody - 30;
         CoinManager.Coin = CoinManager.Coin - 30;
         MetalManager.Metal = MetalManager.Metal - 30;
         Cam_Placement.turretN = Cam_Placement.turretN + 1;
     }
     
     public void enemyspawn () {
         Debug.Log("Base");
         hasPlaced = false;            
         currentBuilding = TheTarget= ((GameObject)Instantiate (enemyOB)).transform;
         Camy.enabled = false;
     }
 
 
 }
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

31 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

Related Questions

I want to make a weapon follow the player after i instatiate the weaponinto the game. 1 Answer

Game Object doesn't instantiate at Mouse Position 3 Answers

Instantiating random prefabs gives wrong position 3 Answers

*Solved* Loop instantiating objects causes my Unity Editor to crash 1 Answer

Instantiate particle system attached to object 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