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

Collider rotation problem when instantiating prefab

Discussion in 'Scripting' started by advo, Nov 18, 2020.

  1. advo

    advo

    Joined:
    Apr 6, 2016
    Posts:
    14
    I have a prefab (its a train..) with a box collider. The prefab has the train mesh, facing the Z axis, with a box collider with size of (0.7, 0.7, 14.2).

    14.2 represents essentially the length of the train.

    First version of code, this prints out the box collider bounds as if the train was not rotated...
    Quaternion rotation = Quaternion.LookRotation(Vector3.left);           
    obj = Instantiate(prefab);
    obj.trans.rotation = rotation;
    //This prints (0.7, 0.7, 14.2)
    Debug.Log("Current collider bounds size " + obj.GetComponent<BoxCollider>().bounds.size);



    Second Version of code, this prints the box collider bounds with the expected value...

    Quaternion rotation = Quaternion.LookRotation(Vector3.left);
    obj = Instantiate(prefab, position, rotation);
    //This prints (14.2, 0.7, 0.7) <- Why is this different from the first version of code??
    Debug.Log("Current collider bounds size " + obj.GetComponent<BoxCollider>().bounds.size);


    Why is my collider not rotating in the first version of the code?
     
    Last edited: Nov 18, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  3. advo

    advo

    Joined:
    Apr 6, 2016
    Posts:
    14
    Hi Kurt - if i understand what u're saying, yes its in line with what i'm expecting, that the world bounding volume would change with the rotation. But it only seems to change in the 2nd example of code, and not the first. I am trying to understand why.