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. Dismiss Notice

Getting the angle from Transform.RotateAround

Discussion in 'Scripting' started by Cratthorax, Dec 16, 2020.

  1. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Good day,

    I'm using Transform.RotateAround to rotate several smaller Stars, around a bigger Star, in a multiple Star System. In order to allow editing any of the smaller Star components, I'd set their initial (speed01 * Time.deltaTime) to = 0, and return it to its initial value after I leave selection of said smaller Star.

    The problem arises when trying to limit this edit, to the currently selected Star object, which is a child object inside the bigger Star prefab. Changing the speed will apply the value to any smaller Star type, in all Star systems with multiple Stars, and said Star component enabled.

    So, to make a long story short: is there a way to calculate the "angle" of Transform.RotateAround?

    See the code snippet inisde the update block:

    Code (csharp):
    1.  
    2.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3.         if (Physics.Raycast (ray, out hit, Mathf.Infinity) && compSelected == false) {
    4.             if (hit.collider == this.component01.collider) {
    5.                 component01.GetComponent<Renderer> ().material.color = Color.blue;
    6.             }
    7.         }
    8.      
    9.         if (Physics.Raycast (ray, out hit, Mathf.Infinity) && Input.GetMouseButtonDown (0)) {
    10.             if (hit.collider == this.component01.collider) {
    11.                 if(compSelected == false){
    12.                     compSelected = true;
    13.                     component01.GetComponent<Renderer> ().material.color = Color.green;
    14.                     speed01 = 0;
    15.                     system.selectionSounds[0].Play(0);
    16.                 } else if(compSelected == true){
    17.                     compSelected = false;
    18.                     component01.GetComponent<Renderer> ().material.color = compColorDefault01;
    19.                     speed01 = 66;
    20.                     system.selectionSounds[1].Play(0);
    21.                 }
    22.             }
    23.         }
     
  2. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Found the answer. You'd need to add a check for the script component int, to apply it inside the function, like that:

    Code (CSharp):
    1. int speed = this.transform.GetComponent<system>().speed01;
    correction: it needs to be inside the start block to get the initial speed. Nested inside the update block, it will catch it's latest speed obviously.
     
    Last edited: Dec 17, 2020