[SOLVED] How to instantiate an object X distance away towards angle?

I want to create a star / pentagram around a character.

There’s many ways! :slight_smile: Here’s a function I like, I find I use it quite a bit:

    Vector2 GetPointOnCircle(Vector2 origin, float radius, float angle) {

        float angleInRadians = angle * Mathf.Deg2Rad;

        var x = origin.x + radius * Mathf.Sin(angleInRadians);
        var y = origin.y + radius * Mathf.Cos(angleInRadians);

        return new Vector2(x,y);

    }

So, I imagine for a pentagram, you could do something like this:

using UnityEngine;
using System.Collections;

//Place the PentagramMaker component on the character in question
public class PentagonMaker : MonoBehaviour {

    const float innerRadius = 5f;
    const float outerRadius = 10f;
    const int num_points = 10;

    public GameObject pentagramPointPrefab;

    void Start() {

        for(var i = 0; i < num_points; i++) {

            //We'll say every odd point is going to be an inner point;
            bool is_inner = (i % 2 != 0);
            float radius = (is_inner) ? innerRadius : outerRadius;

            //Chop it up so each point is evenly displaced around the circle;
            float angle = (360f / (float)num_points) * i;

            Vector2 newPointLocation = GetPointOnCircle (transform.position, radius, angle);

            //Create a new pentagram, parent it to this character.
            var pentagramPoint = Instantiate(pentagramPointPrefab, newPointLocation, Quaternion.identity) as GameObject;
            pentagramPoint.transform.parent = transform;
        }


        //Remove the pentagon maker when we're done with it.
        Destroy (this);

    }


    Vector2 GetPointOnCircle(Vector2 origin, float radius, float angle) {

        float angleInRadians = angle * Mathf.Deg2Rad;

        var x = origin.x + radius * Mathf.Sin(angleInRadians);
        var y = origin.y + radius * Mathf.Cos(angleInRadians);

        return new Vector2(x,y);

    }
}

That being said, I would make a sprite or a model “pentagram mesh” and instantiate that on the player. Unless your going to be doing something with these points, there’s no reason to be creating them procedurally.

1 Like

Oh thanks that’ll be useful and your idea is a great one. I suppose I could create a pentagram around the player and give it a custom collider to check if an enemy is touching the pentagram.

Yup, that would work way better!