- Home /
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. 
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 ?
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.
Thanks! This is just what i am looking for. $$anonymous$$y turret works great now.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                