Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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

Discussion in 'Scripting' started by Gr00vy_, Sep 25, 2022.

  1. Gr00vy_

    Gr00vy_

    Joined:
    Aug 2, 2022
    Posts:
    10
    Hello everybody, I'm french so I apologize in advance for maybe some rude langage mistakes. :oops:

    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 : https://gyazo.com/fee39082a328e3e4fbcd26f8bb7433cd

    My C# source code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraTracer : MonoBehaviour
    6. {
    7.     public LineRenderer[] _arrayLines = new LineRenderer[90];
    8.     private SpriteRenderer _spriteRenderer;
    9.     private int _numberOfLines;
    10.     private float _angle;
    11.     private float _rayLength;
    12.     void Start()
    13.     {
    14.         _spriteRenderer = GetComponent<SpriteRenderer>();
    15.         _angle = _spriteRenderer.transform.eulerAngles.z;
    16.         _rayLength = 3f;
    17.  
    18.         _numberOfLines = 90;
    19.  
    20.         for (int i = 0 ; i < _numberOfLines ; i++)
    21.         {
    22.             GameObject go = new GameObject("Line");
    23.             LineRenderer lr = go.AddComponent<LineRenderer>();
    24.             _arrayLines[i] = go.GetComponent<LineRenderer>();
    25.  
    26.             _arrayLines[i].positionCount = 2;
    27.             _arrayLines[i].SetPosition(0, _spriteRenderer.transform.position);
    28.             _arrayLines[i].SetPosition(1, new Vector3((_spriteRenderer.transform.position.x+_rayLength)*dCos(_angle)-i*9f,
    29.                                                       (_spriteRenderer.transform.position.y+_rayLength)*dSin(_angle)-i*9f, _spriteRenderer.transform.position.z));
    30.             _arrayLines[i].startWidth = 0.01f;
    31.             _arrayLines[i].endWidth = 0.01f;
    32.         }
    33.     }
    34.  
    35.     void Update()
    36.     {
    37.         _angle = _spriteRenderer.transform.eulerAngles.z;
    38.         for (int i = 0 ; i < _numberOfLines ; i++)
    39.         {
    40.             _arrayLines[i].SetPosition(0, _spriteRenderer.transform.position);
    41.             _arrayLines[i].SetPosition(1, new Vector3((_spriteRenderer.transform.position.x+_rayLength)*dCos(_angle)-i*9f,
    42.                                                       (_spriteRenderer.transform.position.y+_rayLength)*dSin(_angle)-i*9f, _spriteRenderer.transform.position.z));
    43.         }
    44.  
    45.     }
    46.  
    47.     public float dCos(float angle) { return Mathf.Cos(angle)*180f/3.14f; }
    48.     public float dSin(float angle) { return Mathf.Sin(angle)*180f/3.14f; }
    49. }
    50.  

    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 :eek:
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,558
    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:

    Code (csharp):
    1. for (int i = 0; i <= 90; i++)
    2. {
    3.   // generate the rotation around Z+
    4.   Quaternion rotation = Quaternion.Euler( 0, 0, i);
    5.  
    6.   // generate positions in an arc
    7.   // this works by multiplying Vector3.right by the rotation to rotate the vector
    8.   // Vector3.zero is just a center
    9.   Vector3 position = Vector3.zero + rotation * Vector3.right;
    10.  
    11.   Debug.Log( position.ToString());
    12. }
     
  3. Gr00vy_

    Gr00vy_

    Joined:
    Aug 2, 2022
    Posts:
    10
    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.
     
  4. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,015
    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.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    29,558
    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()
     
  6. Gr00vy_

    Gr00vy_

    Joined:
    Aug 2, 2022
    Posts:
    10
    Thanks again for your helps guys. I found myself the way I want to do it :)

    Here my new code :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class CameraTracer : MonoBehaviour
    5. {
    6.     public LineRenderer[] _arrayLines = new LineRenderer[90];
    7.     private SpriteRenderer _spriteRenderer;
    8.     private int _numberOfLines;
    9.     private float _angle;
    10.     private float _rayLength;
    11.     void Start()
    12.     {
    13.         _spriteRenderer = GetComponent<SpriteRenderer>();
    14.         _rayLength = 3f;
    15.         _numberOfLines = 90;
    16.         for (int i = 0 ; i < _numberOfLines ; i++)
    17.         {
    18.             GameObject go = new GameObject("Line");
    19.             LineRenderer lr = go.AddComponent<LineRenderer>();
    20.             _arrayLines[i] = go.GetComponent<LineRenderer>();
    21.             _arrayLines[i].positionCount = 2;
    22.             _arrayLines[i].startWidth = 0.01f;
    23.             _arrayLines[i].endWidth = 0.01f;
    24.         }
    25.     }
    26.     void Update()
    27.     {
    28.         _angle = transform.eulerAngles.z;
    29.         _angle = (_angle*Mathf.PI)/180f;
    30.  
    31.         for (int i = 0 ; i < _numberOfLines ; i++)
    32.         {
    33.             _arrayLines[i].SetPosition(0, _spriteRenderer.transform.position);
    34.             _arrayLines[i].SetPosition(1, new Vector3(_rayLength*Mathf.Cos((_angle-2.36f - 0.01744f*i))+_spriteRenderer.transform.position.x,
    35.                                                       _rayLength*Mathf.Sin((_angle-2.36f - 0.01744f*i))+_spriteRenderer.transform.position.y, _spriteRenderer.transform.position.z));
    36.         }
    37.     }
    38. }
    Here my desired final render :

    https://gyazo.com/b8c9e5707e6080e05faa7770e293df39

    Thanks again guys :)
     
    Last edited: Sep 26, 2022
  7. Gr00vy_

    Gr00vy_

    Joined:
    Aug 2, 2022
    Posts:
    10
    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 :D )
     
unityunity