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 /
This question was closed Mar 04, 2016 at 08:01 PM by Son_ofSpartacus for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Son_ofSpartacus · Mar 04, 2016 at 12:07 PM · rotationscripting problemquaternionenemy ai

AI targeting player in complete wrong direction

So, I have a 'tank' enemy in a game I am making in Unity2d. I made a simple script to make the turret part of the tank rotate towards the player. The turret did not face the player correctly, so I put it inside an empty game object I can rotate around so that the turret faces the correct direction, and put the targeting script inside the empty game object. The problem is that no matter which way I rotate it, the turret never faces the player correctly. It will sometimes orient itself correctly but then it turn 180 degrees as soon as the tank starts following the player or until it bumps into any walls. Here is the script, and how I can I get the turret to face the player 100% of the time?

 using UnityEngine;
 using System.Collections;
 
 public class EnemyTurret : MonoBehaviour {
 
     public Transform target;
 
     void Update () {
         if (target != null)
         {
             Quaternion rotation = Quaternion.LookRotation(target.position - transform.position, transform.TransformDirection(Vector3.up));
             rotation.x = 0;
             rotation.y = 0;
             transform.rotation = rotation;
         }
 
     }
 
 }

I also fear it might have something to do with the chassis' AI follow script as well, so I'm also putting it in if you wanna take a look. Ignore the commented out sections, thats for some multiple target stuff I'm working on.

 using UnityEngine;
 using System.Collections;
 
 public class TankAI : MonoBehaviour
 {
 
     public Transform target;
     //public Transform secondTarget;
     public float speed = 3f;
     public float engageDistance = 3f;
     public float ignoreDistance = 10f;
 
 
     void LookAtTarget()
     {
 
         if (target != null)
         {
             transform.LookAt(target.position);
             transform.Rotate(new Vector3(0, -90, 0), Spaaaaaace.Self);
             transform.Rotate(new Vector3(speed * Time.deltaTime, 0, 0));
         }
 
         /*if (target = null)
         {
             transform.LookAt(secondTarget.position);
             transform.Rotate(new Vector3(0, -90, 0), Spaaaaaace.Self);
             transform.Rotate(new Vector3(speed * Time.deltaTime, 0, 0));
         }/*/
     }
 
 
     // Update is called once per frame
     void Update()
     {
 
 
 
         if (target != null)
         {
             if (Vector3.Distance(transform.position, target.position) <= (ignoreDistance))
             {
                 if (Vector3.Distance(transform.position, target.position) > (engageDistance))
                 {
                     transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
                     LookAtTarget();
                 }
             }
         }
 
 
         /*if (target = null)
         {
             if (Vector3.Distance(transform.position, secondTarget.position) <= (ignoreDistance))
             {
                 if (Vector3.Distance(transform.position, secondTarget.position) > (engageDistance))
                 {
                     transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
                     LookAtTarget();
                 }
             }
         }/*/
 
 
 
 
     }
 }

EDIT: I've noticed I'm wasn't entirely clear with what was going on. I've rotated the turret object and the sprite so that they SHOULD be rotating to face the player, but they're facing 180 degrees away from the player some/most of the time, and occasionally rotating correctly.

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

  • Sort: 
avatar image
1
Best Answer

Answer by Exceptione · Mar 04, 2016 at 12:10 PM

Had a similar problem, this may be of help to you:

http://answers.unity3d.com/questions/654222/make-sprite-look-at-vector2-in-unity-2d-1.html

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 Son_ofSpartacus · Mar 04, 2016 at 08:01 PM 1
Share

Thanks! That submission you linked is just a huge hub of solutions to this problem. Awesome!

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Quaternion snaps back to original position 2 Answers

Rotation script isn't working 1 Answer

Getting a 2D object to face the direction of its velocity relative to other objects in the level 0 Answers

How do I smoothly rotate the GameObejcts with negative 90 degrees 0 Answers

How do I rotate the GameObejcts smoothly with negative degrees 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