Search Unity

Rotation and Scaling why?

Discussion in 'Scripting' started by MG, Apr 1, 2015.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I have this code:

    The target is a object with the tag named enemy

    Code (CSharp):
    1. if (target)
    2.         {
    3.             if(target.gameObject.tag == "enemy")
    4.             {
    5.                 // Rotate object to the target
    6.                 Vector3 relativePos = target.position - transform.position;
    7.                 Quaternion rotation = Quaternion.LookRotation (relativePos);
    8.                 ball.transform.rotation = rotation;
    9.             }
    10.            
    11.             if(Time.time >= nextFireTime    )
    12.             {
    13.                 FireProjectile();
    14.             }
    15.         }
    I dont know why but the code was ment to just rotate the object, see the result in the video:

     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Because the parent object has a scale
     
  3. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    This is probably because you have a non-uniform scale on some parent to the turret.

    edit: What hpjohn said ^^
     
  4. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    So how do i fix it?
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    Put your rotating object under a parent (or hierarchy) that isn't scaled.
     
  6. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Here is the hierarchy of the object
     

    Attached Files:

  7. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I assume "ball" is the object rotating?
    You need to set the scale of Turret and base to (1, 1, 1)
    If base needs to be scaled, you will have to put ball directly under Turret, not as a child to base.
     
  8. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    ThermalFusion, thanks that help! love to you!
     
    ThermalFusion likes this.