Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Instantiating breaks LookAt BUG!

Discussion in 'Scripting' started by SleepyGhost, Jul 22, 2014.

  1. SleepyGhost

    SleepyGhost

    Joined:
    May 7, 2014
    Posts:
    7
    When the player prefab is in the scene he perfectly aims at the cursor, using LookAt, but when I remove the player prefab from the scene and Instantiate him from a script, he now has an offset to his aim.

    Why does the clone have an aim offset, but the prefab does not... This seems like a bug with the LookAt.
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FaceMouseOnClick : MonoBehaviour
    6. {
    7.  
    8.     //Components
    9.     private SkillsManager s;
    10.  
    11.     //Vairables
    12.     public float speed = 2.0f;
    13.  
    14.     public LayerMask layerMask;
    15.  
    16.     void Start () {
    17.         s = GetComponent<SkillsManager> ();
    18.     }
    19.     //Call the method in LateUpdate so we can affect the bones after the animation, otherwise bones can not be translated.
    20.     void LateUpdate () {
    21.         LookAtTarget();
    22.     }
    23.  
    24.  
    25.     public void LookAtTarget ()    {
    26.  
    27.         //Raycast from camera to mouse
    28.         RaycastHit hit;
    29.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    30.         //Debug.DrawRay(ray.origin, ray.direction * 100, Color.red);
    31.  
    32.         //Plane for Player Rotation
    33.         Plane playerPlane = new Plane(Vector3.up, transform.position);
    34.         float hitdist = 0.0f;
    35.  
    36.         //Rotate Player
    37.         if (playerPlane.Raycast (ray, out hitdist)) {
    38.      
    39.             if (Input.GetMouseButton (0)) {
    40.  
    41.                 Vector3 targetPoint = ray.GetPoint(hitdist);
    42.                 Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    43.                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.time);
    44.             }
    45.         }
    46.  
    47.         //Weapon LookAt Cursor
    48.         if (Physics.Raycast (ray, out hit, 50.0f, layerMask)) {
    49.             float distance = Vector3.Distance (transform.position, hit.point);
    50.  
    51.             if (Input.GetMouseButton (0) && distance > 4.5f) {
    52.                 s.handHold.LookAt (hit.point);
    53.                 //    Debug.Log ("Distance = " + distance);
    54.             }
    55.         }
    56.     }
    57.  
    58. }
    59.  
    Correct me If I'm wrong, but this has my programmer and I stumped. Is there a bug with LookAt and Instantiating?
     
    Last edited: Jul 22, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    A) use [ code] tags [ /code]

    B) The most crucial parts for solving this are going to be your Instantiation code, and the way your prefab is set up versus the way the object was set up when it was in the scene. I'd give pretty good odds that there is some difference in the transform's values or something like that that would account for an offset.
     
  3. SleepyGhost

    SleepyGhost

    Joined:
    May 7, 2014
    Posts:
    7
    Thank you for the reply! The prefabs values and the instantiated clone are identical, the only difference is one has an aim offset, with no explanation. I have zeroed it down to this script, I am completely baffled as to why the clone would have an offset all of a sudden, when the prefab dragged directly into the scene does not.. which leads me to believe that LookAt has a bug when coupled with an Instantiated object.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Can you post a screenshot? Hard to see the problem from code
     
  5. SleepyGhost

    SleepyGhost

    Joined:
    May 7, 2014
    Posts:
    7

    Here's the problem, and its only when I instantiate him using this code from the Spawer.cs.
    Code (csharp):
    1.     Instantiate(obj, transform.position, transform.rotation);
     
  6. Skeibl

    Skeibl

    Joined:
    Jul 22, 2014
    Posts:
    1
    There is no difference between the object in the scene and the instantiated one. We discovered the problem with the instantiated object. Dragging the prefrab directly into the scene and running it allows us to use the controls properly while the instantiated one still cannot aim right.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    The chances of there being a bug in LookAt related to how an object arrives in the world are slim. Something else is wrong.
     
  8. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Where do you update the cursors image position? Perhaps your gun is aiming at the correct place, but your cursor image is not. For instance, if the anchor point of your cursor image is not in the center, it will be offset by the corresponding amount
     
  9. SleepyGhost

    SleepyGhost

    Joined:
    May 7, 2014
    Posts:
    7
    @A.Killingbeck, I already tried that, good idea tho. That is all perfectly working.

    @KelsoMRK, Indeed, I'm just frustrated, running out of ideas.

    The instantiated clone apparently does not care about this line here,
    Code (csharp):
    1. s.handHold.LookAt(hit.point);
    and I do not know why. When I comment that line out, they both aim with an offset but they both shoot at the same spot. Thank you guys for your responses.
     
  10. SleepyGhost

    SleepyGhost

    Joined:
    May 7, 2014
    Posts:
    7
    It may actually be this line in the SkillsManager script
    Code (csharp):
    1.  
    2.         currentWeapon = Instantiate(weaponsArray[i],handHold.position,handHold.rotation) as Weapon;
    3.         currentWeapon.transform.parent = handHold;
    I do not believe the weapon is becoming a child of that transform correctly.. the instantiated object should follow the bone handHold but it does not! I am missing something here, but its between these two scripts surely, and those two lines. Any insight about how instantiated objects parent-child relationship works would be helpful as the documentation has not helped me to solve this. Thank you!
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    You should then consider that any position/rotation you fed into the Instantiate function will have been in global space - you'll probably want to set the transform.localPosition and localRotation manually after setting the parent. Are you doing that?
     
  12. SleepyGhost

    SleepyGhost

    Joined:
    May 7, 2014
    Posts:
    7
    @StarManta. thank you for your response, although I'm not sure I follow.

    I still wonder why only the clone is offset, while the prefab operates normally. It's starting to feel like the instantiate method is broken somehow, I made an empty object and made it a child of the handHold bone, he follows the new empty child perfectly... but, that does not explain why he wont follow the handHold bone after instantiation.

    Something in the way that instantiate works with bones is somehow bugged. The model is an FBX from blender, exported with Y up just like unity. I just find it odd that it works perfectly without instantiating the prefab, I even tried loading from resources folder bypassing the drag and drop to the Spawner component. Thank you for your time and input guys!
     
  13. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Why do you keep saying it's a unity bug? ;p It's 99.99% not a Unity bug, as you are doing something which would normally be extremely trivial, which 1000's of people have done before and had no problems. Can you post the skillsmanager script aswell?