Odd Forward Direction Positioning Issue

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. I spawn the sphere like this:

var newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

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 character (the script is attached to my game object so “transform” is the character’s game object’s transform.

I have a test scene I used to develop my animation sequence and it works fine in that scene. The sphere spawns directly in front of the character no matter which way the character’s parent game object is rotated.

When I put an instance of the same prefab in the real scene, for some reason, the above code doesn’t place the sphere in front of the game object 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 game object. In-game, in the test scene, if I rotate the character 360 degrees the line is drawn in front of the game object no matter where the game object is facing, however in the real scene it only draws lines away from the camera. If the game object is facing the camera, the line is drawn directly behind the game object.

Here’s a screenshot showing the line drawn away from the character in the real scene but in front of my character in the test scene.

Some other things I have tried / checked:

  • Disabled all camera effects in case
    something on the camera was doing it
  • Ensured I’m rotating the GameObject
    in both cases, not just a child
    element

I’m just looking for tips on things I can check because I’m not sure what to look at next. I haven’t encountered this or something like this in Unity before.

Thanks all!

OK this was a stupid one. I was focused on the fact that it worked in one scene and not another and assumed that meant my positioning code was right. A closer look revealed that, stupidly, the code was wrong and the reason it appeared as though one scene was working was just because I had the character positioned at 0,0,0 in that scene. :S

Here’s the right code, btw

var newSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);

newSphere.transform.position = transform.position + (transform.forward * 5) + new Vector3(0, 1, 0);