En este momento estás viendo ¿Cómo generar objetos aleatorios dentro del área del círculo dibujado?

 – Unity

¿Cómo generar objetos aleatorios dentro del área del círculo dibujado? – Unity

¿Cómo generar objetos aleatorios dentro del área del círculo dibujado?

– UnityAssets3Free

buenas , por aqui kein y esta vez os traigo
nueva pregunta

El primer script dibuja un círculo en el que puedo controlar el tamaño del radio y hacer que el círculo sea más pequeño o más ancho:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[ExecuteAlways]
[RequireComponent(typeof(UnityEngine.LineRenderer))]
public class DrawCircle : MonoBehaviour

    [Range(1, 50)] public int segments = 50;
    [Range(1, 500)] public float xRadius = 5;
    [Range(1, 500)] public float yRadius = 5;
    [Range(0.1f, 5)] public float width = 0.1f;
    [Range(0, 100)] public float height = 0;
    public bool controlBothXradiusYradius = false;
    public bool draw = true;

    [SerializeField] private LayerMask targetLayers;
    [SerializeField] private LineRenderer line;

    private void Start()
    
        if (!line) line = GetComponent<LineRenderer>();

        if (draw)
            CreatePoints();
    

    private void Update()
    
        if (Physics.CheckSphere(transform.position, xRadius, targetLayers))
        
            Debug.Log("player detected");
        
        else
        
            Debug.Log("player NOT detected");
        
    

    public void CreatePoints()
    
        line.enabled = true;
        line.widthMultiplier = width;
        line.useWorldSpace = false;
        line.widthMultiplier = width;
        line.positionCount = segments + 1;

        float x;
        float y;

        var angle = 20f;
        var points = new Vector3[segments + 1];

        for (int i = 0; i < segments + 1; i++)
        
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xRadius;
            y = Mathf.Cos(Mathf.Deg2Rad * angle) * yRadius;

            points[i] = new Vector3(x, height, y);

            angle += (380f / segments);
        

        // it's way more efficient to do this in one go!
        line.SetPositions(points);
    

#if UNITY_EDITOR
    private float prevXRadius, prevYRadius;
    private int prevSegments;
    private float prevWidth;
    private float prevHeight;

    private void OnValidate()
    
        // Can't set up our line if the user hasn't connected it yet.
        if (!line) line = GetComponent<LineRenderer>();
        if (!line) return;

        if (!draw)
        
            // instead simply disable the component
            line.enabled = false;
        
        else
        
    
#endif

El segundo script está generando objetos, estoy usando el control deslizante de rango para cambiar la cantidad de objetos que se generarán:

ahora quiero poder usar ambos scripts para poder generar los objetos dentro del área del círculo dibujado cuando cambio el control deslizante de la cantidad de objetos para generar la variable numberOfObjects generará los objetos dentro del área del círculo dibujado y lo hará coloque los objetos en el terreno dependiendo de la altura del terreno.

1 respuesta 1

A partir de la respuesta propuesta, solo necesitaría aleatorizar el radio y repetir la llamada al método para tantos objetos como desee. Igual a:

private void SpawnSphereOnEdgeRandomly3D(float maxRadius)

    float radius = Random.Range(-0f, maxRadius);
    Vector3 randomPos = Random.insideUnitSphere * radius;
    randomPos += transform.position;
    randomPos.y = 0f;
        
    Vector3 direction = randomPos - transform.position;
    direction.Normalize();
        
    float dotProduct = Vector3.Dot(transform.forward, direction);
    float dotProductAngle = Mathf.Acos(dotProduct / transform.forward.magnitude * direction.magnitude);
        
    randomPos.x = Mathf.Cos(dotProductAngle) * radius + transform.position.x;
    randomPos.z = Mathf.Sin(dotProductAngle * (Random.value > 0.5f ? 1f : -1f)) * radius + transform.position.z;
        
    GameObject go = Instantiate(_spherePrefab, randomPos, Quaternion.identity);
    go.transform.position = randomPos;

nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias

sin mas,hasta la proxima

Deja una respuesta