-1

Hi im working on a character animation / interaction with the environment. Im trying to spawn rocks of different sizes going from the ground to where the gravity direction is applied.

Im using c# for both of my scripts (character_movements_animation.cs & powerUp.cs)

My question is how to spawn objects around my character and not through it. Im using the code below:

/* Variables Declaration */
public GameObject rock_Small;
public GameObject rock_Medium;
public GameObject rock_Large;

private float posX, posY, posZ;
private bool checkPos = false;
//Use this for initialization
void Start() {
    //Empty for now
}

// Update is called once per frame
void Update() {

    if (Random.Range(0, 100) < 10) {

        checkPos = false;
        posX = this.transform.position.x + Random.Range(-5.0f, 5.0f);
        posY = this.transform.position.y;
        posZ = this.transform.position.z + Random.Range(-5.0f, 5.0f);

        if(posX > 3f && posY > 3f){
            checkPos = true;
        }

        if (checkPos == true) {

            Vector3 newPos = new Vector3(posX, posY, posZ);
            Instantiate(rock_Small, newPos, rock_Small.transform.rotation);
        }

    }
}

Also see the example in the figure. UNITY

4

1 回答 1

0

似乎您想在角色周围生成对象,但要避免角色自己的体积。

我建议这样的事情:

var pos = this.transform.position = Random.insideUnitCircle.normlaized * _distance; //how far away from your character. 1, based on your original code

这将在您的角色周围创建一个环。如果你想进一步随机化,你可以对这个值应用更小的随机偏移量,例如:

var pos = this.transform.position = Random.insideUnitCircle.normlaized * _distance;
pos += Random.insideUnitCircle * _offset; //how wide the ring should be. 0.1 is probably sufficient.
于 2018-12-10T18:22:35.053 回答