Search Unity

Instantiating objects randomly spawns on different (not given) position

Discussion in 'Scripting' started by egartnuc, Oct 8, 2018.

  1. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    Hello sirs and madames,

    2 issues that to me seem they should not happen in a world of math and logic. But here they are.

    I'm instantiating a gameobject, in this case an axe, and as I do that I push it. Below code.

    Code (CSharp):
    1.             Vector3 instPos = new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);
    2.             Debug.Log(instPos);
    3.             GameObject axe = Instantiate(thrownAxe, instPos, Quaternion.identity) as GameObject;
    4.             axe.gameObject.GetComponent<Rigidbody>().AddRelativeForce(Camera.main.transform.forward * throwForce);
    However.

    Problem 1: The axes spawn on 2 different Y-positions. 90% of the time, roughly, in the right spot, but sometimes above. Illustrated in this picture, and my paint skills show the separate paths they travel. I would estimate roughly 1-2 Y world units difference between them.


    Edit: I can't seem to add this picture correctly, so here's the link (https://imgur.com/a/Chz8PJ4)

    Problem 2:
    As I spam a bunch of axes in the air (because it's fun), the ones that spawn in the correct position, they actually start spawning lower and lower and lower and lower until they spawn in the ground.

    Other info:
    - The script repeatedly spawning this prefab axe is put on the axe in hand you see in the picture, which is in a static position (for now) on my FPS-controller.
    - There is no other mention or manipulation of the instPos vector3 in other places of the code.

    I'm grateful for any help. Have a good one.

    Best regards,
    Tomas
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add "Debug.Log(instPos.y.ToString());" to the code above and see if the Y position of instPos is dropping unexpectedly. The Debug.Log line you are currently using will significantly round the values due to how Vector3.ToString() is implemented.

    I'd also investigate if colliders on the axes themselves are causing them to be pushed to unexpected positions when instantiated. You can eliminate that pretty quickly if you just repeat your test with all the colliders on the axe prefab disabled.
     
  3. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    Hello, @Joe-Censored and thank you for your response.

    I followed your instructions and the values were indeed changing. So with this little pointer I started looking around. I knew there were no colliders on any of these objects, however that something was (in the words of Bubbles) F***y was certain. And lo and behold. Gravity was on on the rigidbodies of both the "origin" axe, and the instantiated axe. Once I turned off the one I instantiated them on, the problem went away. It doesn't explain it fully, but hey, problem solved.

    So basically the falling spawn point was because of gravity on the origin object. However it didn't visually move on runtime, I guess because it was held in place by a parent or something. But maybe some background unity physics applied regardless. Maybe a bug. As far as the ones spawning above goes, I think that was totally a bug, coming from the same problem somehow.

    Anyhow, problem solved. Again, Joe, thank you for your time.
     
    Joe-Censored likes this.