I am trying to create points on the edge of my 2d capsule, I show an example of what I am looking for in the image.
I need 30 points where I will instantiate objects; They will need to be placed on the edge of the capsule and arranged evenly, the design is organized so that I have 5left, 10top, 5right, 10bottom points
Can someone help me?
Draft code:
using UnityEngine;
[ExecuteInEditMode]
public class Place : MonoBehaviour
{
public int numberOfPoints = 30;
public float circleRadius = 0.1f;
private void OnDrawGizmos()
{
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer == null)
{
Debug.LogError("SpriteRender is missing!");
return;
}
if (spriteRenderer != null)
{
Bounds bounds = spriteRenderer.bounds;
Vector3 center = bounds.center;
Vector3 size = bounds.size;
float angleStep = 360f / numberOfPoints;
for (int i = 0; i < numberOfPoints; i++)
{
float angle = i * angleStep * Mathf.Deg2Rad;
Vector3 direction = new Vector3(Mathf.Cos(angle), Mathf.Sin(angle), 0) * (size.x / 2);
Gizmos.color = Color.red;
Gizmos.DrawLine(center, direction);
Vector3 circlePosition = center + direction;
Gizmos.DrawWireSphere(circlePosition, circleRadius);
}
}
}
}
If it’s for one item, just make the points by hand, save a prefab with those as child GameObjects.
If it’s for a bunch of these that are different in some mathematically-definable way, then it may be worth writing some code.
Otherwise, traditionally a “capsule” usually implies two circles connected by a tube… yours above looks more parabolic on its end.
But in any case, the same rules apply: you would write code to consider the linear perimeter, perhaps a generator function, first for the top straight part, one curve, then the bottom straight part going the opposite direction, then the other curve going the opposite direction.
Here’s a cheap and cheerful example of how I would start the process with a generator:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// @kurtdekker - to demo using generators to make points
//
// To use: drop on empty GameObject sufficiently far ahead of a Camera
//
// tinker with the intervals and counts to get what you need
//
public class DemoGenerator : MonoBehaviour
{
IEnumerable<Vector3> GeneratePoints()
{
// TODO: add whatever you want in here
Vector3 position = Vector3.zero;
// straight line
int count = 10;
for (int i = 0; i < count; i++)
{
position = new Vector3( i, 0);
yield return position;
}
// now a half circle
count = 10;
float radius = 5;
Vector3 center = position + Vector3.up * radius;
for (int i = 0; i < count; i++)
{
float angle = (i * 180.0f) / count;
Quaternion rotator = Quaternion.Euler(0, 0, angle);
Vector3 offset = Vector3.down * radius;
offset = rotator * offset;
position = center + offset;
yield return position;
}
/// etc...
}
// run it with a coroutine only so we can watch it
IEnumerator Start ()
{
foreach( var position in GeneratePoints())
{
GameObject sphere = GameObject.CreatePrimitive( PrimitiveType.Sphere);
Vector3 finalPosition = position + Vector3.forward * 10;
sphere.transform.localPosition = finalPosition;
yield return new WaitForSeconds( 0.2f);
}
}
}