Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SpawnInLineFromRotationHelper : MonoBehaviour {
- public Vector3 startingRotation;
- public int numMidPoints = 5;
- public float distance = 5f;
- [Header("Prefabs")]
- public GameObject startPrefab;
- public GameObject midPrefab;
- public GameObject endPrefab;
- List<GameObject> spawnedObjs = new List<GameObject>();
- void Update () {
- for(int i = 0; i < spawnedObjs.Count; i++)
- {
- Destroy(spawnedObjs[i]);
- }
- spawnedObjs.Clear();
- Quaternion myRotation = Quaternion.Euler(startingRotation); //Convert to quat for the problems sake
- SpawnInLineFromRotation(transform.position, myRotation);
- Debug.DrawRay(transform.position, myRotation * Vector3.forward);
- }
- void SpawnInLineFromRotation(Vector3 startPoint, Quaternion rotation)
- {
- Vector3 forward = rotation * Vector3.forward;
- //Start point
- GameObject startObj = (GameObject)Instantiate(startPrefab, startPoint, rotation);
- spawnedObjs.Add(startObj);
- float stepSize = distance / (float)(numMidPoints + 1);
- //Start at 1, add 2 to numMidPoints, and end 1 early to make step size calculation easier
- for(int i = 0; i < numMidPoints; i++)
- {
- Vector3 pos = startPoint + (forward * stepSize * (float)(i+1));
- GameObject midObj = (GameObject)Instantiate(midPrefab, pos, rotation);
- spawnedObjs.Add(midObj);
- }
- //End point
- GameObject endObj = (GameObject)Instantiate(endPrefab, startPoint + (forward * distance), rotation);
- spawnedObjs.Add(endObj);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement