• 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
1
Question by Frizack400 · Jul 04, 2017 at 10:03 AM · 2drotationquaternion

Using quaternions in 2D to follow a rotation. Both question and an Answer.

Hello so I have been looking around for a way of making a turret turn to face a object as it moves past it for a bit now. I look all over the internet, and quite a lot of it seemed to consist of using quaternions to do this plus lots of other stuff. I am new to unity and have not had the time to full research quaternions. So for not I came up with my own solution. This is an answer because I think I figured out a really easy way and a question because it seemed to easy to do and is there any downside to doing it this way?

I had two game objects. 1. Real turret head. (This one had the sprite on) 2. Fake turret head. I had a script attacked to fake turret head. that said

public Transform enemyTarget; public Transform lookAtPos; public Transform fakeRotator; public Vector3 WorldPos;

 private CircleCollider2D turretAgroRange;
 
 // Use this for initialization
 void Start () 
 {
     turretAgroRange = this.GetComponent<CircleCollider2D>();
 }
 
 // Update is called once per frame
 void Update () 
 {
     //this.transform.LookAt(enemyTarget);

     //transform.LookAt(transform.position + new Vector3(0,0,1),enemyTarget);
 
     Vector3 relativePos = enemyTarget.position - transform.position;
     Quaternion tempQuat = Quaternion.LookRotation(relativePos);
     Quaternion tempFloat = fakeRotator.rotation;
     fakeRotator.rotation = Quaternion.LookRotation(relativePos);
     


     print (tempQuat);
     print (transform.rotation + "2");

 }


 void OnTriggerEnter2D(Collider2D col)
 {
     print ("Went into agro range");
     enemyTarget = col.transform;
 }    


This would make the fake turret head look at an object as it moved past. Now I originally did this for for the real turret head. But as i'm sure people who have had the same problem know it would just turn on its side and you could not see the sprite.

So what I did after this, I made the real turret head a child of the fake on. so when the fake would turn so did the real. But still I had the same issue of it turning so all I did was make the fake turret's starting Y rotation of - 90. Then set the real turret head to to -90. Making it balance out. (This is to say that the front of the real turret. is facing down the y axis, you would have to change the directions depending on where yours is.)

So I hope this helps anyone else. If anyone has any ideas why this might not be a good idea for some reason please let me know. :D

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 Paratrooper82 · Jan 30, 2018 at 03:06 AM 0
Share

Here is my question to this. I have a frog that jumps towards the left, as soon as the player is visible. When the frog hits a .tag named "wall" it will go towards the right, and vice versa.

How would I switch its image depending on which direction (left or right) it is facing?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

177 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 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

Problem with projectile rotation - 2D 0 Answers

Set Instanatiated Object's Rotation Towards Player 1 Answer

2D TopDown rotating a gun according to its parent position, ON MOBILE, not PC, 0 Answers

Shortest Rotation Path at Constant Speed 1 Answer

Getting a 2D object to face the direction of its velocity relative to other objects in the level 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