Search Unity

SOLVED C# LookRotation problem

Discussion in 'Scripting' started by NoniGames, Jul 12, 2015.

  1. NoniGames

    NoniGames

    Joined:
    Jul 6, 2015
    Posts:
    3
    I am trying to make a turret gun rotate towards the mouse. The game is in 2D and the the turret gun is under turret gameobject rotated 90 degrees on x axis and 180 degrees on y axis and the gun has local rotation of 180 degrees on y axis and 180 degrees on z axis. So I want the lookrotation to only rotate on local y axis of the gun. Tried looking for solutions around but couldn't find any. Hope someone can help me with this. Also, if you need more information, go ahead and ask.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class testi : MonoBehaviour {
    5.     public Vector3 newDir;
    6.     public bool mouseObj;
    7.     public Transform target;
    8.  
    9.     void Start () {
    10.      
    11.     }
    12.  
    13.     void Update () {
    14.         if(!mouseObj){
    15.             Vector3 targetDir = target.position - transform.position;
    16.             newDir = Vector3.RotateTowards(transform.up, targetDir, 1 * Time.deltaTime, 0.0F);
    17.             transform.rotation = Quaternion.LookRotation(newDir);
    18.         }else{
    19.             Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    20.             pos[2] = 0;
    21.             transform.position = pos;
    22.         }
    23.     }
    24. }
     
    Last edited: Jul 12, 2015
  2. NoniGames

    NoniGames

    Joined:
    Jul 6, 2015
    Posts:
    3
    Nevermind, I got the issue fixed by adding a "testObj" GameObject into the scene at the same position as the turret gameobject and then changing the code to
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class turret : MonoBehaviour {
    6.  
    7.     public Transform rotObj;
    8.    
    9.     void Start () {
    10.        
    11.     }
    12.    
    13.     void Update () {
    14.         if(transform.localEulerAngles[1] < 150){
    15.             transform.localEulerAngles = new Vector3(0, 150, 180);
    16.         }
    17.         if(transform.localEulerAngles[1] > 210){
    18.             transform.localEulerAngles = new Vector3(0, 210, 180);
    19.         }
    20.  
    21.         Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    22.         pos[2] = 0;
    23.        
    24.         Vector3 targetDir = pos - transform.position;
    25.         Vector3 newDir = Vector3.RotateTowards(-transform.right, targetDir, 0.5f * Time.deltaTime, 0.0F);
    26.         rotObj.rotation = Quaternion.LookRotation(newDir);
    27.         if(rotObj.localEulerAngles[1]-90 > 150 && rotObj.localEulerAngles[1]-90 < 210){
    28.             transform.localEulerAngles = new Vector3(0, rotObj.localEulerAngles[1]-90, 180);
    29.         }
    30.     }
    31. }
    32.  
     
    Last edited: Jul 13, 2015
  3. TokyoDan

    TokyoDan

    Joined:
    Jun 16, 2012
    Posts:
    1,080
    I am trying to follow your example but I got some questions. Your explanation says 2D.

    Is it simple 2D top-down? x = east/west, Y= north/south, Z=not used (or maybe used for layering)
    Is it 2D top-down view in 3D? x = east/west, Z= north/south, Y=not used (or maybe used for layering)
    or simple 2D side view: x = east/west, Y=up/down, Z= depth (for things in background like parallax scrolling)?
     
  4. NoniGames

    NoniGames

    Joined:
    Jul 6, 2015
    Posts:
    3
    Well, it's x = west/east, y = up/down and z = layering, but I'm not sure if the code is adaptable like that. If it isn't, tell me and I'll see if I can help. (Also, sorry replying took so long... I just recently started checking this email's inbox.)