• 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 Fiinix · Mar 22, 2014 at 03:17 PM · 2dturretfacingtower defense

2D Tower Defense: Turret facing enemy

Hi,

I am trying to play a bit around in Unity to make a 2D Tower Defense. I have enemies moving along a path and i want the turret to face enemies in range. I use a list for enemies currently in range of the turret and the turret should face at the direction of the first element in that list. Here is my code for the rotation:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TurnToEnemy : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         Transform target = getEnemyInQueue();
         if (target != null)
         {
             Quaternion rotation = Quaternion.LookRotation(target.position - transform.position, transform.TransformDirection(Vector3.up));
             rotation.x = 0;
             rotation.y = 0;
             transform.rotation = rotation;
         }
     }
 
     Transform getEnemyInQueue()
     {
         List<Transform> enemyQueue = GetComponent<EnemyQueue>().enemyQueue;
         if (enemyQueue.Count != 0)
         {
             return GetComponent<EnemyQueue>().enemyQueue[0];
         }
         return null;
     }
 }

As the turret I have a simple sprite with a circle and a rectangular as cannon. The turret rotates in the direction of one enemy but with either its front (the side the "cannon" is facing to) or its back whatever is nearer to the enemy. The turret should snap and stick to the enemy so a smooth movement is not intended. Can anyone help me with that?

Comment
Add comment · Show 2
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 robertbu · Mar 22, 2014 at 03:22 PM 0
Share

What axis are the turrets rotating on? Are the turret axes aligned or at an angle? Turrets where the axis of rotation does not align with a world axes requires a different solution than one with the they are axes aligned.

avatar image Fiinix · Mar 22, 2014 at 03:41 PM 0
Share

Ok problem is solved. The x-axis should turn around the z-axis. Now i used the Quaternion.FromToRotation() method to rotate and it works. Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pedronox · Nov 07, 2016 at 07:41 AM

Quaternion rotation = Quaternion.LookRotation(target.position - transform.position, transform.TransformDirection(Vector3.back));

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

21 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

Related Questions

tower defense turret turn right side by 90 degree 0 Answers

how to make an object go back to its starting position if it doesen't hit a target? 1 Answer

2D mouse/touch controlled turret problems 0 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Detect if player is facing an Object 1 Answer

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