How can I projecting constant rays depending a Z degrees angle ?

Hello everybody, I’m french so I apologize in advance for maybe some rude langage mistakes. :sweat_smile:

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.

Here is my problem : Screen capture - fee39082a328e3e4fbcd26f8bb7433cd - Gyazo

My C# source code :

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 :hushed:

Your image host isn’t supported in my browser. Just attach photos here if you want people to see them.

Reading generally from .eulerAngles will only work in very constrained situations, not the general case.

All about Euler angles and rotations, by StarManta:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

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 :slight_smile:
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.

Unity - Scripting API: Transform.forward (unity3d.com)
“When a GameObject is rotated, the blue arrow representing the Z axis of the GameObject also changes direction. Transform.forward moves the GameObject in the blue arrow’s axis (Z).”

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.

What do you think a Quaternion is?

If you want to do sin/cos, go nuts by all means. That’s all that is happening on Quaternion.Euler()

Thanks again for your helps guys. I found myself the way I want to do it :slight_smile:

Here my new code :

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>();
        _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].startWidth = 0.01f;
            _arrayLines[i].endWidth = 0.01f;
        }
    }
    void Update()
    {
        _angle = transform.eulerAngles.z;
        _angle = (_angle*Mathf.PI)/180f;

        for (int i = 0 ; i < _numberOfLines ; i++)
        {
            _arrayLines[i].SetPosition(0, _spriteRenderer.transform.position);
            _arrayLines[i].SetPosition(1, new Vector3(_rayLength*Mathf.Cos((_angle-2.36f - 0.01744f*i))+_spriteRenderer.transform.position.x,
                                                      _rayLength*Mathf.Sin((_angle-2.36f - 0.01744f*i))+_spriteRenderer.transform.position.y, _spriteRenderer.transform.position.z));
        }
    }
}

Here my desired final render :

Thanks again guys :slight_smile:

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 :smile: )