Hello everybody, I’m french so I apologize in advance for maybe some rude langage mistakes.
I’m actually attempting to project 90 rays ( it’s the angle of my projector ) regularly depending of my Z rotation ( like I mentionned on the title ), I want, when I’m rotating the main object, that these 90 LineRenderer rotate correctly with the same space between them on all the rotation, like a RayCasting system.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraTracer : MonoBehaviour
{
public LineRenderer[] _arrayLines = new LineRenderer[90];
private SpriteRenderer _spriteRenderer;
private int _numberOfLines;
private float _angle;
private float _rayLength;
void Start()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
_angle = _spriteRenderer.transform.eulerAngles.z;
_rayLength = 3f;
_numberOfLines = 90;
for (int i = 0 ; i < _numberOfLines ; i++)
{
GameObject go = new GameObject("Line");
LineRenderer lr = go.AddComponent<LineRenderer>();
_arrayLines[i] = go.GetComponent<LineRenderer>();
_arrayLines[i].positionCount = 2;
_arrayLines[i].SetPosition(0, _spriteRenderer.transform.position);
_arrayLines[i].SetPosition(1, new Vector3((_spriteRenderer.transform.position.x+_rayLength)*dCos(_angle)-i*9f,
(_spriteRenderer.transform.position.y+_rayLength)*dSin(_angle)-i*9f, _spriteRenderer.transform.position.z));
_arrayLines[i].startWidth = 0.01f;
_arrayLines[i].endWidth = 0.01f;
}
}
void Update()
{
_angle = _spriteRenderer.transform.eulerAngles.z;
for (int i = 0 ; i < _numberOfLines ; i++)
{
_arrayLines[i].SetPosition(0, _spriteRenderer.transform.position);
_arrayLines[i].SetPosition(1, new Vector3((_spriteRenderer.transform.position.x+_rayLength)*dCos(_angle)-i*9f,
(_spriteRenderer.transform.position.y+_rayLength)*dSin(_angle)-i*9f, _spriteRenderer.transform.position.z));
}
}
public float dCos(float angle) { return Mathf.Cos(angle)*180f/3.14f; }
public float dSin(float angle) { return Mathf.Sin(angle)*180f/3.14f; }
}
I know, my code isn’t optimized at all but for the moment, it’s not the purpose, this is the algorithm who blow my mind and I want to know how can I achieve this objective ? I want to process like this like a challenging coding and not use all-made Unity function ( if it’s exist ) that I cannot reproduce myself.
Thanks a lot for your eventual [huge] help, I tried to fix this all my afternoon so I decided to yell some HELP
You can sweep degrees with a regular for loop and produce rotations:
for (int i = 0; i <= 90; i++)
{
// generate the rotation around Z+
Quaternion rotation = Quaternion.Euler( 0, 0, i);
// generate positions in an arc
// this works by multiplying Vector3.right by the rotation to rotate the vector
// Vector3.zero is just a center
Vector3 position = Vector3.zero + rotation * Vector3.right;
Debug.Log( position.ToString());
}
Hello Kurt, thanks you very much for your devotion
However, I really want to understand and succeed to solve my problem with pure mathematics algorithm, for self-educational and pleasure to learn so your answer do not really help me… Thanks again bro.
The engine knows all of these directions if the object exists in the hierarchy. And so therefore obtaining the data is contradictive to using the engines objects, unless of course the object didn’t exist, such as; as if I was only rendering a mesh and didn’t host that mesh in a hierarchy.
But since you have a transform gameobject, then you are merely talking about constantly raycasting in your transform.forward direction.
The main problem wasn’t unity or some things I can read sometimes on many forums like " set useWorldSpace to false " or " Without matrix you can’t done it ", the main problem was just me and my ignorance on trigonometrics principles.
In definitive, I learned a lot ( after many hours of hair pulling & some rage moments )