• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by halimozturk · Aug 13, 2014 at 09:18 PM · followtargetturretrotatearoundpivot

Using iTween's Turret Example on 2D environment

I am trying to use iTween's Turret Example. I did everything good so far except following mouse position with rotating turret. alt text

I have a turret as you can see in the image above and I want it to follow the target(Mouse Position) by rotating around it self. For that, iTween has great example but it is only for 3d :( So, I tried to implement it in 2D but whatever I did I couldn't succeed.

 using UnityEngine;
 using System.Collections;
 
 public class CannonController : MonoBehaviour
 {
         public Rigidbody2D bullet;
         public Camera mainCam;
         public GameObject pivot;
         float pivotX;
         float pivotY;
 
 
         public Transform target;
         float smoothTime = 0.3f;
         float xOffset = 1f;
         float yOffset = 1f;
         Vector2 velocity;
         Transform thisTransform;
          
 
 
         void Start ()
         {
                 thisTransform = transform;
         }
         // Update is called once per frame
         void Update ()
         {
                 pivotX = pivot.transform.position.x;
                 pivotY = pivot.transform.position.y;
 
 
                 //rotation:
                 Vector2 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
                 mousePos.y = 0;
                 gameObject.transform.rotation = Quaternion.Euler (new Vector3 (mousePos.x, 0, mousePos.y));
                 Vector2 worldPos = Camera.main.ScreenToWorldPoint (mousePos);
                 iTween.LookUpdate (gameObject, iTween.Hash ("looktarget", worldPos, "time", 2, "axis", "y"));
 
 
                 //rotation:
 //                Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
 //                mousePos.z = 0;
 //                gameObject.transform.position = mousePos;
 //                Vector3 worldPos = Camera.main.ScreenToWorldPoint (mousePos);
 //                iTween.LookUpdate (gameObject, iTween.Hash ("looktarget", worldPos, "time", 2, "axis", "y"));
         
                 //fire:
                 if (Input.GetMouseButtonDown (0)) {
                         Rigidbody2D clone = (Rigidbody2D)Instantiate (bullet, new Vector2 (pivotX, pivotY), transform.rotation);
                         clone.velocity = transform.TransformDirection (Vector2.up * 10f);
                 }
         }
 }
 

Commented parts under "rotation:" was the iTween's original code for rotating by following mouse position for 3D environment. The other parts under "rotation:" with Vector2 code is my code.

This was the closest trial I could ever made, but I it is not really working. It only moves the turret on x-axis just a little bit (not much!) But I guess, the turret should be rotate around z-axis. So, can anyone help me to rotate the turret around z-axis smoothly by following mouse position ?

ekran resmi 2014-08-10 21.02.19.png (227.2 kB)
Comment

People who like this

0 Show 0
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

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Aug 14, 2014 at 06:40 AM

I assume you want an object to point at the mouse position.

 Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);
 Vector3 dir = Input.mousePosition - pos;
 float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This code assumes that the 'front' of your turret points right when the rotation is (0,0,0). The arrows in your drawing are a bit confusing. If you are rotating around the central point with the object, make the visible object a child of an empty game object at the pivot. The empty game object will follow the mouse.

Comment

People who like this

0 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 halimozturk · Aug 14, 2014 at 05:38 PM 0
Share

Thanks! This is just what i am looking for. My turret works great now.

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

2 People are following this question.

avatar image avatar image

Related Questions

HELP! i want to write to look at the target and give a force to follow 3 Answers

character controller that follow a target 1 Answer

problems with quaternion rotations 4 Answers

Make the turret automatically rotate to look at where the crosshair is aimed at. 3 Answers

Coding equivalent of Draging an Object into the Inspector (Solved) 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