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

Can't position object in front of another object in one of my scenes

Discussion in 'General Graphics' started by omatase, Apr 9, 2016.

  1. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    I have an effect that looks kind of like a ribbon. When the effect begins I position the start of the ribbon on my character's hand and the end of the effect on a sphere that I spawn directly in front of the character. My CharacterScript is attached to a root Game Object in the hierarchy and is a prefab. I spawn the sphere like this in the CharacterScript:

    Code (CSharp):
    1. var newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    2.  
    3. newSphere.transform.position = (transform.forward * 5) + new Vector3(0, 1, 0);
    As you can see I'm putting it in a position directly in front of my prefab.

    I have a test scene I used to develop my animation sequence. I drag my prefab into that scene and it works fine in that scene. The sphere spawns directly in front of the prefab no matter which way the prefab is rotated.

    When I put an instance of the same prefab in the "final" scene, for some reason, the sphere isn't always placed in front of my prefab unless the game object is facing in the 180 degree range away from the camera.

    To test this, I put a Debug.Drawline call to draw a line directly in front of my prefab. In-game, in the test scene, if I rotate the character 360 degrees the line is drawn in front of the prefab no matter where the prefab is facing, however in the final scene it only draws lines away from the camera. If the prefab is facing the camera, the line is drawn directly behind the prefab.

    Here's a screenshot showing the line drawn away from the prefab in the final scene but in front of my prefab in the test scene. No, I'm not rotating anything except the root game object (again, the game object from which my debug line is being drawn and the sphere is being spawned).

    direction issue.jpg

    Here is the code I'm using to draw the debug line.

    Code (CSharp):
    1. Debug.DrawLine(transform.position + new Vector3(0, 1, 0), (transform.forward * 10) + new Vector3(0, 1, 0), Color.red);
    2.  
    Some other things I have tried / checked:

    • Disabled all camera effects in case something on the camera was doing it

    Any feedback on something I can look at next? Nothing I'm aware of in Unity quirks would do something like this.

    I added some animated gifs that show the debug line only drawing away from the camera in one scene while drawing in all directions in the other scene.

    https://gyazo.com/3705941eeb72efbf087b291431bb8112 (only draws away)
    https://gyazo.com/90dadeaec268b3a1f5c875ad7909a560 (draws all directions)

    Thanks all!
     
    Last edited: Apr 9, 2016
  2. cblarsen

    cblarsen

    Joined:
    Mar 10, 2007
    Posts:
    266
    It looks like you have forgotten to add transform.position to the position of your sphere.
    Shouldn't it be
    Code (csharp):
    1.  
    2. var newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    3. newSphere.transform.position = transform.position + (transform.forward * 5) + new Vector3(0, 1, 0);
    4.  
     
    Moondrakor and omatase like this.
  3. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    Oh my goodness, that was it! Thanks so much. I know this was a simple thing but it threw me off in my test scene because I had my character positioned at (0,0,0).