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

Resolved Making a Sweep minimap

Discussion in 'Scripting' started by enesbagci2332, Aug 20, 2023.

  1. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    I'm trying to make a game minimap. But there is a problem, when I try to send the enemies' positions, it's quite broken.



    At first, the minimap would only show pings at the front, now on everywhere except the front. I'm quite new to Unity UI.

    Can you developers help me out?

    This is the radar search on the player. Basically, there is a huge trigger that will ping enemies' location on trigger enter.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RadarSearch : MonoBehaviour
    4. {
    5.     public GameObject player;
    6.  
    7.     public GameObject enemyUIBeep;
    8.     public GameObject rocketLauncherBeep;
    9.  
    10.     public GameObject beepParent;
    11.  
    12.     private void OnTriggerEnter(Collider other)
    13.     {
    14.         if (other.CompareTag("Enemy"))
    15.         {
    16.             float addXValue = (other.transform.position.x - player.transform.position.x) / 5;
    17.             float addZValue = (other.transform.position.z - player.transform.position.z) / 5;
    18.  
    19.             GameObject beep = Instantiate(enemyUIBeep, beepParent.transform.position, beepParent.transform.rotation, beepParent.transform);
    20.  
    21.             Vector3 shouldValue = new Vector3(beepParent.transform.position.x + addXValue, beepParent.transform.position.y + addZValue);
    22.  
    23.             beep.GetComponent<RectTransform>().position = shouldValue;
    24.         }
    25.     }
    26. }

    And this is the rotation copy for copying the player's rotation to the UI's minimap.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class RotationCopy : MonoBehaviour
    4. {
    5.     public Transform getThisRotation;
    6.  
    7.     // Update is called once per frame
    8.     private void Update()
    9.     {
    10.         transform.rotation = new Quaternion(transform.rotation.x, transform.rotation.y, getThisRotation.rotation.y, getThisRotation.rotation.w);
    11.     }
    12. }

    I'm a newbie, sorry for taking your time.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Manipulating UI positions in code is ALWAYs difficult. It's best to put invisible anchors in your UI hierarchy for useful parts, such as "center" and "edge" and then drag references to those RectTransforms into your code. Don't use magic numbers like dividing by 5 because you'll never be correct on all machines.

    Simplify the problem: make 1 enemy make him AT the player's location, get all the math right to show him dead center.

    Then move him forward from the player X units, where X units is the intended radar distance, and get that working.

    Now rotate and verify you got the rotation correct.

    Make sure before you do ANYTHING that you have the anchoring and scaling correct for what you want, or else you'll only end up redoing it.

    Here are some notes on UI Anchoring, Scaling, CanvasScaler, etc:

    https://forum.unity.com/threads/inc...size-between-two-people.1130146/#post-7261747

    https://forum.unity.com/threads/game-ui-button-size-problem.1142650/#post-7337383

    Usually you need to choose a suitable ScaleMode and MatchMode in the Canvas Scaler and stick with it 100%. Generally if you change those settings you will often need to redo your UI entirely.

    I also use this
    CanvasScalerOrientationDriver
    utility to make sharing UI for Landscape / Portrait easier. Read what it does carefully.

    https://gist.github.com/kurtdekker/8802b1b6c708637398f8c9167641efd3

    Beyond that...

    Time to start debugging! Here is how you can begin your exciting new debugging adventures:

    You must find a way to get the information you need in order to reason about what the problem is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling
    Debug.Log()
    statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the names of the GameObjects or Components involved?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as
    Debug.Log("Problem!",this);


    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    If your problem is with OnCollision-type functions, print the name of what is passed in!

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    "When in doubt, print it out!(tm)" - Kurt Dekker (and many others)

    Note: the
    print()
    function is an alias for Debug.Log() provided by the MonoBehaviour class.
     
    enesbagci2332 likes this.
  3. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    First don't try to manipulate a Quaternion by it's variables, always use a function designed to manipulate them.

    I'm not sure where this is used, but
    transform.rotation = targetsRotation;
    is more proper, if the target is supposed to be the same rotation.
     
    enesbagci2332 likes this.
  4. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I found this tutorial, see if anything he goes over might help :)
     
    enesbagci2332 likes this.
  5. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    I tried to do that tutorial but there were some problems I encounter, so I took the textures from files and tried writing my own scripts.




    I write that because otherwise, it will change x,y without me wanting to change them.


    Also, I started to see your face in every thread I create, thanks for eveything, dude!
     
    Last edited: Aug 20, 2023
    wideeyenow_unity likes this.
  6. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82


    I will inspect the links you sent, and thanks for the information!
     
  7. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    So I inspected @Kurt-Dekker send. I wanted to add that there are no bugs. I don't know how to convert 3D to 2D correctly because when I turn it doesn't at the place that I want it to be. The problem is there.
     
  8. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Well, the thing is, with tutorials, you have to do them exactly as they are(even though it's not fully what you want). But having a working example, and of course making it yourself, will help your knowledge to expand on all the different aspects.

    I made the same mistake of only wanting to do what I wanted to do in the beginning, and tried taking a bit from here and a bit from there from multiple tutorials.. And boom, talk about a headache and nothing working right or making any sense.

    But the trick is to just get the tutorial working... Then once you have a better understanding of what piece of code does what(and why), then you can take your own snippets and make a new creation with it. Especially, see what it depended on or what depends on it, to work, by simply commenting that part of the code out(when you come back later and forgot what was what).
     
    enesbagci2332 likes this.
  9. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    you wouldn't have to make a full new project, just make a new scene and call it TestScene or something, that way all your prefabs and scripts are within the same project :)
     
    enesbagci2332 likes this.
  10. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    Thanks for the information dude, I will!
     
  11. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    To update you guys on the problem: I think the problem is whether the player is looking at the enemies or not. To fix this I tried copying the player's rotation to the UI but it doesn't remember that it's rotation is changed. It just places it in the distance value I have given it. In:


    Code (CSharp):
    1. float addXValue = (other.transform.position.x - player.transform.position.x) / 4;
    2. float addZValue = (other.transform.position.z - player.transform.position.z) / 4;
    3.  
    4. GameObject beep = Instantiate(enemyUIBeep, beepParent.transform.position, beepParent.transform.rotation, beepParent.transform);
    5.  
    6. Vector3 shouldValue = new Vector3(beepParent.transform.position.x + addXValue, beepParent.transform.position.y + addZValue, beepParent.transform.position.z);
    7.  
    8. beep.GetComponent<RectTransform>().position = shouldValue;
    To fix this maybe I can add the rotation value to the shouldValue. But how can I do that?
     
  12. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    I just added euler angle of y to my code and it is fixed:


    Code (CSharp):
    1. float addXValue = (other.transform.position.x - player.transform.position.x) / 4;
    2. float addZValue = ((other.transform.position.z - player.transform.position.z) + player.transform.rotation.eulerAngles.y / 4) / 4;
    3.  
    4. GameObject beep = Instantiate(enemyUIBeep, beepParent.transform.position, beepParent.transform.rotation, beepParent.transform);
    5.  
    6. Vector3 shouldValue = new Vector3(beepParent.transform.position.x + addXValue, beepParent.transform.position.y + addZValue, beepParent.transform.position.z);
    7.  
    8. beep.GetComponent<RectTransform>().position = shouldValue;

    What solved my problem was using Debug.log(), thanks for the remainder @Kurt-Dekker you are a great person! Also thanks @wideeyenow_unity !
     
    Kurt-Dekker likes this.
  13. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    As Kurt would say, "when it doubt, print it out", lol..

    But yup, print out things as they happen is a great way to debug, as is Debug.DrawRay/Line when it comes to rotations, so you can physically see the angles and what-not. Debugging is very important!

    But glad you got it to work, kudos! :)
     
    enesbagci2332 likes this.
  14. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    Update with the minimap's code in case someone finds this post later!


    Code (CSharp):
    1. if (other.CompareTag("Enemy"))
    2. {
    3.     float addXValue = (other.transform.position.x - player.transform.position.x + player.transform.rotation.eulerAngles.y / 2) / 4;
    4.  
    5.     float addZValue = (other.transform.position.z - player.transform.position.z + player.transform.rotation.eulerAngles.y / 4) / 4;
    6.  
    7.     //VECTOR3.FORWARD KULLANARAK BEEPPARENT'IN ÖNÜNÜN KONUMUNU FALAN AL SONRA * İLE addXValue/addZValue 'ye ekle
    8.  
    9.     //Debug.Log(player.transform.forward);
    10.     //Debug.Log(other.transform.position);
    11.  
    12.     float rotationValue = compass.transform.rotation.z;
    13.  
    14.     Vector3 shouldValue = new Vector3((beepParent.transform.position.x + addXValue * rotationValue), (beepParent.transform.position.y + addZValue * rotationValue), 0);
    15.  
    16.     GameObject beep = Instantiate(enemyUIBeep, beepParent.transform.position, beepParent.transform.rotation, beepParent.transform);
    17.  
    18.     //Debug.Log(beepParent.transform.up);
    19.  
    20.     beep.GetComponent<RectTransform>().position = shouldValue; //+ (beepParent.transform.TransformPoint(beepParent.transform.up) / 20);
    21. }