Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 unity_Ms0xAEdw2qrBBg · Sep 11, 2020 at 12:01 AM · quaternion.lookrotationquaternion.slerptower defense

Help with tower defense game using LookRotation

 void LookAtEnemy()
     {
         GameObject[] enemies;
         enemies = GameObject.FindGameObjectsWithTag("Enemy");
 
         float speed = 0.05f;
 
         foreach (GameObject enemy in enemies)
         {
             Vector3 enemyPos = enemy.transform.position;
             enemyPos.y = transform.position.y;
             Vector3 relativePos = enemyPos - transform.position;
 
             Quaternion rotation = Quaternion.LookRotation(relativePos);
 
             float distance = Vector3.Distance(enemyPos, transform.position);
 
             if (distance < range)
             {
                 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed);
             }
         }
     }    

Here is the script which makes a turret to look at enemies. It's kind of working, but not accurate. A turret looks at enemies AND spaces between enemies. I don't want it, and I have no idea what to do. If you want more detailed information, please comment.

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 N-8-D-e-v · Sep 11, 2020 at 12:05 AM

when dealing with look rotation, you need to get a directional vector. So, in the line where you declare the vector3 called relativePos, change it to this

 Vector3 relativePos = (enemyPos - transform.position).normalized;

This should fix it, if you have other questions or would like me to explain more then just ask!

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
avatar image
0

Answer by unity_Ms0xAEdw2qrBBg · Sep 11, 2020 at 12:14 AM

Thank you very much for kind reply. I tried, however, it's not still working. Nothing has changed. I will explain more. There are 3 enemies spawning. A turret looks at the first enemy when it enters the range, and if it leaves, a turret looks the middle between the first enemy and the second enemy. Then looks at the second one, and so on. What's wrong with that?

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 N-8-D-e-v · Sep 11, 2020 at 04:49 PM 0
Share

you are using Quaternion.Slerp, which will sphereically interpolate between two rotations, meaning that to get from one rotation to another, it will have to smoothly (or not so smoothly) rotate between them, so just change transform.rotation = Quaternion.Slerp(transform.rotation, rotation, speed); to transform.rotation = rotation Also, when using Slerp, make sure to multiply speed by Time.deltaTime to make it frame-rate-independent

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

133 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

Related Questions

Smooth LookAt OnTriggerEnter for static enemies 1 Answer

Quaternion Slerp 180 Away from target 1 Answer

How to make character rotate in the direction of movement and direction of the camera? 1 Answer

Rotating gameObject to aim at target using Quaternion 1 Answer

Quaternion.Slerp() behaves weirdly when turning to look at moving object 2 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