SHARE
TWEET

Untitled

a guest Jun 26th, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpawnInLineFromRotationHelper : MonoBehaviour {
  6.  
  7.     public Vector3 startingRotation;
  8.     public int numMidPoints = 5;
  9.     public float distance = 5f;
  10.  
  11.     [Header("Prefabs")]
  12.     public GameObject startPrefab;
  13.     public GameObject midPrefab;
  14.     public GameObject endPrefab;
  15.  
  16.     List<GameObject> spawnedObjs = new List<GameObject>();
  17.  
  18.     void Update () {
  19.         for(int i = 0; i < spawnedObjs.Count; i++)
  20.         {
  21.             Destroy(spawnedObjs[i]);
  22.         }
  23.         spawnedObjs.Clear();
  24.         Quaternion myRotation = Quaternion.Euler(startingRotation); //Convert to quat for the problems sake
  25.         SpawnInLineFromRotation(transform.position, myRotation);
  26.         Debug.DrawRay(transform.position, myRotation * Vector3.forward);
  27.     }
  28.    
  29.     void SpawnInLineFromRotation(Vector3 startPoint, Quaternion rotation)
  30.     {
  31.         Vector3 forward = rotation * Vector3.forward;
  32.  
  33.         //Start point
  34.         GameObject startObj = (GameObject)Instantiate(startPrefab, startPoint, rotation);
  35.         spawnedObjs.Add(startObj);
  36.  
  37.         float stepSize = distance / (float)(numMidPoints + 1);
  38.  
  39.         //Start at 1, add 2 to numMidPoints, and end 1 early to make step size calculation easier
  40.         for(int i = 0; i < numMidPoints; i++)
  41.         {
  42.             Vector3 pos = startPoint + (forward * stepSize * (float)(i+1));
  43.             GameObject midObj = (GameObject)Instantiate(midPrefab, pos, rotation);
  44.             spawnedObjs.Add(midObj);
  45.         }
  46.  
  47.         //End point
  48.         GameObject endObj = (GameObject)Instantiate(endPrefab, startPoint + (forward * distance), rotation);
  49.         spawnedObjs.Add(endObj);
  50.     }
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top