Search Unity

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

Discussion in 'Scripting' started by Deleted User, Dec 21, 2014.

  1. Deleted User

    Deleted User

    Guest

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

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    There's many ways! :) Here's a function I like, I find I use it quite a bit:

    Code (CSharp):
    1.     Vector2 GetPointOnCircle(Vector2 origin, float radius, float angle) {
    2.  
    3.         float angleInRadians = angle * Mathf.Deg2Rad;
    4.  
    5.         var x = origin.x + radius * Mathf.Sin(angleInRadians);
    6.         var y = origin.y + radius * Mathf.Cos(angleInRadians);
    7.  
    8.         return new Vector2(x,y);
    9.  
    10.     }
    So, I imagine for a pentagram, you could do something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. //Place the PentagramMaker component on the character in question
    5. public class PentagonMaker : MonoBehaviour {
    6.  
    7.     const float innerRadius = 5f;
    8.     const float outerRadius = 10f;
    9.     const int num_points = 10;
    10.  
    11.     public GameObject pentagramPointPrefab;
    12.  
    13.     void Start() {
    14.  
    15.         for(var i = 0; i < num_points; i++) {
    16.  
    17.             //We'll say every odd point is going to be an inner point;
    18.             bool is_inner = (i % 2 != 0);
    19.             float radius = (is_inner) ? innerRadius : outerRadius;
    20.  
    21.             //Chop it up so each point is evenly displaced around the circle;
    22.             float angle = (360f / (float)num_points) * i;
    23.  
    24.             Vector2 newPointLocation = GetPointOnCircle (transform.position, radius, angle);
    25.  
    26.             //Create a new pentagram, parent it to this character.
    27.             var pentagramPoint = Instantiate(pentagramPointPrefab, newPointLocation, Quaternion.identity) as GameObject;
    28.             pentagramPoint.transform.parent = transform;
    29.         }
    30.  
    31.  
    32.         //Remove the pentagon maker when we're done with it.
    33.         Destroy (this);
    34.  
    35.     }
    36.  
    37.  
    38.     Vector2 GetPointOnCircle(Vector2 origin, float radius, float angle) {
    39.  
    40.         float angleInRadians = angle * Mathf.Deg2Rad;
    41.  
    42.         var x = origin.x + radius * Mathf.Sin(angleInRadians);
    43.         var y = origin.y + radius * Mathf.Cos(angleInRadians);
    44.  
    45.         return new Vector2(x,y);
    46.  
    47.     }
    48. }
    49.  
    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.
     
    Deleted User likes this.
  3. Deleted User

    Deleted User

    Guest

    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.
     
  4. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Yup, that would work way better!