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

8 Dir (Don't Ignore)

Discussion in 'General Graphics' started by Nails-of-Scream, Dec 16, 2019.

  1. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    If this get ignored then there's a chance that I'll stop using unity for this kind of stuff.

    Again I have problems with 8 directional sprites and need help. Here's a script I found and tried to use. Does this look right?
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class spriteAngler : MonoBehaviour
    4. {
    5.     public Transform toFace;
    6.     public SpriteRenderer toManipulate;
    7.     public Sprite[] mySprites;
    8.     private float theta;
    9.     private Vector3 a;
    10.     void Update()
    11.     {
    12.         toManipulate.sprite = mySprites[GetAngleIndex()];
    13.     }
    14.     int GetAngleIndex()
    15.     {
    16.         a = toFace.position - transform.position;
    17.  
    18.         a.Normalize();
    19.         var b = transform.forward;
    20.  
    21.         theta = Mathf.Acos(Vector3.Dot(a, b)) * Mathf.Rad2Deg;
    22.  
    23.         if (a.x * a.z < 0)
    24.             theta = 360.0f - theta;
    25.  
    26.         if (theta >= 292.5f && theta < 337.5f)
    27.             return 7;
    28.         else if (theta >= 22.5f && theta < 67.5f)
    29.             return 1;
    30.         else if (theta >= 67.5f && theta < 112.5f)
    31.             return 2;
    32.         else if (theta >= 112.5f && theta < 157.5f)
    33.             return 3;
    34.         else if (theta >= 157.5f && theta < 202.5f)
    35.             return 4;
    36.         else if (theta >= 202.5f && theta < 247.5f)
    37.             return 5;
    38.         else if (theta >= 247.5f && theta < 292.5f)
    39.             return 6;
    40.         else if (theta >= 337.5f || theta < 22.5f)
    41.             return 0;
    42.         else return 0;
    43.     }
    44.  
    45.     private Rect guiPos = new Rect(0, 0, 720, 30);
    46.     void OnGUI()
    47.     {
    48.         GUI.Label(guiPos, "Angle from the Player is: " + theta + " and forward=" + transform.forward + " and vectorToTarget=" + a);
    49.     }
    50. }
    51.  
    Help at-least?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,992
    is there some problem with that script?
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,580
    I ignored, for being vague and none descriptive about potential problem (if any) and silly thread title.
     
    Last edited: Dec 17, 2019
  4. DimitriX89

    DimitriX89

    Joined:
    Jun 3, 2015
    Posts:
    550
    Script looks right. Problem description or goodbye, Felicia.
     
  5. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    I've found a new way but thanks either way.