#Problem with instantiating

1 messages · Page 1 of 1 (latest)

slender vortex
#

Hi! im trying to instantiate a prefab in the same position as a gameobject in the scene, but even though the code has sense, the prefab instantiates in another random position

#

this is where it was supposed to be

burnt basalt
# slender vortex Hi! im trying to instantiate a prefab in the same position as a gameobject in th...

code looks fine

  1. validate Student.transform.position is correct
    now you might assume it is because that's where the visual character is, however, when ragdolling it's common that the character goes away from their origin position, so it might be more consistent to use something like the hip bone as position reference
  2. not directly related to your question, but when ragdolling, camera culling issues can happen with the character. sometimes elements on the character seem to disappear, that is because unity calculates the bounding box with the root position as position reference. you can research this on your own but just pointing it out, one possible approach is to make the bounds of the renderers much bigger for example
    https://answers.unity.com/questions/1741780/skinned-mesh-renderer-doesnt-follow-ragdoll.html
  3. another possibility is that the visual renderer of your spawned puddle is offset from spawned gameobject position the because of how the renderer is placed in the prefab. I don't think this is the case but it's a possibility.
    lastly you can just get a reference to the spawned element and validate that position
    GameObject obj = Instantiate(...);
    Debug.Log(obj.transform.position);
viral furnace
#

Couple of quick unrelated questions:

  1. Why surround position with parentheses? Seems kinda pointless. This isn't the cause of the bug it's just odd to see
  2. Are you using notepad to write code var where is your syntax highlighting
slender vortex
#

but i will try putting the hip bone as reference

#

but i think it may cause a problem, the gameobject will spawn here

#

and its supposed to spawn on the fllor

#

floor*

burnt basalt
#

yeah with the hips, I'd still do something like raycasting against the ground to find a ground point

to validate that's not the issue, you could also visualize the root position for example, like implementing
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDrawGizmos.html
and drawing the position of the character like as a sphere
https://docs.unity3d.com/ScriptReference/Gizmos.DrawSphere.html
you can set the color before it if you like
https://docs.unity3d.com/ScriptReference/Gizmos-color.html

#

if duing ragdolling, the body doesn#t fly away or move away from the origin position along z or x, I guess that wouldn't be the issue either then

#

Note: that only shows for the scene view then,
unless, I think, you enable gizmos during play mode in that one tab over the game view

slender vortex
#

I think theres a easier solution for this.... using raycasts is way too complex

#

just for a small script that instantiates