Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Slowly turn to object

Discussion in '2D' started by Vifi, Dec 12, 2014.

  1. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Hi, i am new here and i have a problem that i can't solve.

    I am doing small 2d game shooter of spaceships blablabla doesn't matter.
    I want my ship to shoot autotargeting rocket, you know what i mean.
    So, i am instantiate rocket on my ship position, in rocket script in start i am giving it random rotation on z axis and telling it to go forward (in my case - up) all the time. The only problem i have is to make it slowly rotate to enemy. I tried to rotate it by 1 every frame and stop it when it reach a correct rotation which i wanted to get by look at, but it seems like it is not returning anything. So this is my problem. Can you tip me how to solve it?
    Other thing is i don't rlly understand what is quaternion, and how it is diffrent from rotation.
    Thanks and bye
     
  2. SpeakUpGames

    SpeakUpGames

    Joined:
    Nov 25, 2014
    Posts:
    56
    To get a direction between two objects, you simply need to subtract their positions and normalize it (Note that you might have to switch the order of the objects)
    Code (csharp):
    1. Vector3 dir = Vector3.Normalize(transform1.position - transform2.position);
    Once you have that, perhaps a method such as RotateTowards could be useful for you:
    http://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html
     
  3. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    Well, it works, but not all.
    First thing is that my rocket is moving on side. I can solve it by changing model rotation, but still i would like to know why is that happen.
    2nd is that it is rotating on all axis, i tried to make it only z axis, but when i am trying from normalized direction take only z it doesn't work at all. Same when i tried do it after RotateTowards.

    My code:
    Code (CSharp):
    1. public class RakietaPocisk : MonoBehaviour {
    2.  
    3.     public Transform target;
    4.     Vector3 rotacjaCel = Vector3.zero;
    5.     float speed = 10f;
    6.     // Use this for initialization
    7.     void Start () {
    8.         transform.Rotate (new Vector3 (0,0, Random.Range(0,360)));
    9.         target = GameObject.FindGameObjectWithTag ("Przeciwnik").transform;
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if(target == null)
    16.             target = GameObject.FindGameObjectWithTag ("Przeciwnik").transform;
    17.  
    18.        
    19.         rotacjaCel = Vector3.Normalize(target.position - transform.position);
    20.  
    21.         //rotacjaCel = new Vector3 (0, 0, rotacjaCel.z);
    22.  
    23.  
    24.         float step = speed * Time.deltaTime;
    25.         Vector3 newDir = Vector3.RotateTowards(transform.forward, rotacjaCel, step, 1.0F);
    26.  
    27.         Debug.DrawRay(transform.position, newDir, Color.red);
    28.         //newDir = new Vector3 (0, 0, newDir.z);
    29.  
    30.         transform.rotation = Quaternion.LookRotation(newDir);
    31.  
    32.  
    33.         transform.Translate(Vector3.forward *10f* Time.deltaTime);
    34.    
    35.     }
    36. }
    It's not like i want you to solve all my problems, i will try to figure it out on my own but writting this in case if some1 had the same problem and can share the anserw.
     
  4. Vifi

    Vifi

    Joined:
    Aug 5, 2013
    Posts:
    28
    i already solve that problem with :

    Code (CSharp):
    1. Vector3 vectorToTarget = target.position - transform.position;
    2.         float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
    3.         Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
    4.         transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
    5.  
    6.         transform.Translate(Vector3.right *10f* Time.deltaTime);