Positioning objects randomly on a rotated plane

I’m trying to generate some random objects on a rotated plane. Kind of like grass growing from a plane.

Here is the most important function. It returns a random point on the plane.

However it doesn’t work as it’s supposed to. It’s like the more I change the rotation from (0,0,0) the more error it gets. The placed objects seem to be in the right formation but they are not on top of the plane anymore.

Also is there a easier way to do something like this?

This is how I instantiate a new object
GameObject hair = (GameObject) Instantiate(hairType, getRandomPosition(), transform.rotation);

Vector3 getRandomPosition()
    {
        Vector3 vec = transform.position;
        Quaternion qua = transform.rotation;

        Vector3 angles = qua.eulerAngles;

        // spread them around the plane
        vec.x += Random.Range(-1.0f, 1.0f);
        vec.z += Random.Range(-0.5f, 0.5f);

        float oldX = vec.x;
        float oldY = vec.y;
        float oldZ = vec.z;

        float newX;
        float newY;
        float newZ;

        float angX = angles.x*Mathf.Deg2Rad;
        float angY = angles.y*Mathf.Deg2Rad;
        float angZ = angles.z*Mathf.Deg2Rad;

        newY = (oldY * Mathf.Cos(angX)) - (oldZ * Mathf.Sin(angX)); // x axis
        newZ = (oldZ * Mathf.Cos(angX)) + (oldY * Mathf.Sin(angX));

        oldY = newY;
        oldZ = newZ;

        newZ = (oldZ * Mathf.Cos(angY)) - (oldX * Mathf.Sin(angY)); // y axis
        newX = (oldX * Mathf.Cos(angY)) + (oldZ * Mathf.Sin(angY));

        oldZ = newZ;
        oldX = newX;

        newX = (oldX * Mathf.Cos(angZ)) - (oldY * Mathf.Sin(angZ)); // z axis
        newY = (oldY * Mathf.Cos(angZ)) + (oldX * Mathf.Sin(angZ));
        


        return new Vector3(newX, newY, newZ); ;
    }

Try using this function instead:

Vector3 getRandomPosition() 
    { 
    	Vector3 randomPoint = transform.TransformPoint (new Vector3 (Random.Range (-1.0f, 1.0f), 0.0f, Random.Range (-0.5f, 0.5f)));
    	return randomPoint;
    }

Hey awesome, that works. And it’s even a bit more elegant than mine. :smile: I guess I need to comb through the APIs a bit closer.