Search Unity

Simple rotation problem

Discussion in 'Scripting' started by Gomric, Mar 5, 2014.

  1. Gomric

    Gomric

    Joined:
    Dec 30, 2013
    Posts:
    5
    Hello
    Trying to make homing missiles I've encountered weird problems.
    I've simplified it as much as I possibly can, and would really appreciate any help on this.

    Code (csharp):
    1. void Update(){
    2.    homing();
    3.  
    4.    transform.position += this.transform.rotation * Vector3.up * speed * Time.deltaTime; //Propell projectile forward
    5. }
    6.  
    7. private void homing(){
    8.    GameObject target = findClosestTarget();
    9.    Debug.DrawLine(target.transform.position, target.transform.position + new Vector3(10,10,0)); //Confirms that target is valid
    10.    this.transform.LookAt(target.transform); //Problem occurs here. My projectile doesn't seem to turn at all.
    11. }
    this.transform.Rotate(new Vector3(0,0,5));
    Is confirmed to make my projectiles fly in circles.

    Replacing transform.LookAt() with:
    this.transform.rotation = Quaternion.LookRotation(target.transform.position);
    makes my projectiles disappear.
     
    Last edited: Mar 5, 2014
  2. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    120
    The homing fucntion is fine, the issue is that you're not moving the transform forward.

    Give this a shot in the update function.

    Code (csharp):
    1.  
    2. transform.Translate(Vector3.forward * speed * Time.deltaTime);
    3.  
     
  3. Gomric

    Gomric

    Joined:
    Dec 30, 2013
    Posts:
    5
    It's a 2D-project and "up" is indeed forward.
    I guess the problem may be that I have to take this in account when I LookAt().
    Thanks for leading me to that realization, though!
    Any further help on how I solve that matter is still appreciated. Else I'll have to resort into avoiding LookAt() altogether, and do that trigonometry that I rather avoid. ;)
     
  4. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    120
unityunity