Search Unity

Visual Interface

Discussion in 'UGUI & TextMesh Pro' started by Tabb, Nov 23, 2017.

  1. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    Hello all,

    I would like to recreate the interface effect in the following video.
    I'm talking about the arrows that are pointing toward the mouse icon from the center of the screen when the ship turns.



    I'm not sure what would be the best way to do this so I'm in quest for advices :)
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    This requires a wee bit of scripting and math.

    On a screen space UI canvas, add an Image anchored to the center of the screen.

    For example, use these settings:
    • Canvas
      • Image
        Pos (0,0,0)
        Width=160, Height=40
        Pivot X=0, Y=0.5
        Source Image: (your chevron image)

    Then add a script to Image1.

    In Update(), set Image1's Z rotation to the angle between Vector3.left and the vector from the center of the screen to the mouse, and set the width to the mouse vector's magnitude:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. public class Aimer : MonoBehaviour
    4. {
    5.  
    6.     private RectTransform rectTransform;
    7.  
    8.     private void Awake()
    9.     {
    10.         rectTransform = GetComponent<RectTransform>();
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.         var center = new Vector2(Screen.width / 2, Screen.height / 2);
    16.         var mouseVector = new Vector2(center.x - Input.mousePosition.x, center.y - Input.mousePosition.y);
    17.         var angle = Vector2.Angle(Vector2.left, mouseVector);
    18.         if (Input.mousePosition.y < center.y) angle = -angle;
    19.         rectTransform.localRotation = Quaternion.Euler(0, 0, angle);
    20.         rectTransform.sizeDelta = new Vector2(mouseVector.magnitude, 40);
    21.     }
    22.  
    23. }

    Here's an example scene: Aimer_2017-11-24.unitypackage
     
    Last edited: Nov 24, 2017
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    ^ I simplified my reply above.
     
  4. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    Thank you.
    It works perfectly.

    I tried to do the same with an image (attached).
    The only thing I don't know how to do is scaling the sprite to the screen resolution.
     

    Attached Files:

  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Hi,

    You'll want to scale the Canvas to the screen resolution. To do this, add a Canvas Scaler component to the Canvas GameObject.
     
  6. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    I know I'm almost there and I'm missing a little something. But I can't make it works correctly :confused:
     
  7. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
  8. Tabb

    Tabb

    Joined:
    Jan 2, 2015
    Posts:
    40
    I finaly got almost exactly what I wanted.
    Thank you very much for your help.
     
  9. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Glad to help!