Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Vector3 to Transform as a coin spawn target

Discussion in 'Scripting' started by GameLover90, May 23, 2024.

  1. GameLover90

    GameLover90

    Joined:
    Jan 5, 2021
    Posts:
    13
    Hello,

    I am using the script below to spawn coins to a target Vector3. The issue arises with different screen sizes.

    How to convert the target Vector3 to Transform of the target? So that different screen sizes do not represent an issue anymore?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using DG.Tweening;
    4. using UnityEngine;
    5. using UnityEngine.Pool;
    6. using UnityEngine.TextCore.Text;
    7. using UnityEngine.UI;
    8. using static UnityEngine.GraphicsBuffer;
    9. public class CoinController : MonoBehaviour
    10. {
    11.     [SerializeField] private GameObject PileOfCoinsParent;
    12.     [SerializeField] private Text Counter;
    13.     [SerializeField] private Vector3[] InitialPos;
    14.     [SerializeField] private Quaternion[] InitialRotation;
    15.     [SerializeField] private int coinsNumber;
    16.     void Start()
    17.     {
    18.         InitialPos = new Vector3[coinsNumber];
    19.         InitialRotation = new Quaternion[coinsNumber];
    20.         for (int i = 0; i < PileOfCoinsParent.transform.childCount; i++)
    21.         {
    22.             InitialPos = PileOfCoinsParent.transform.GetChild(i).position;
    23.             InitialRotation = PileOfCoinsParent.transform.GetChild(i).rotation;
    24.         }
    25.     }
    26.     private void Reset()
    27.     {
    28.         for (int i = 0; i < PileOfCoinsParent.transform.childCount; i++)
    29.         {
    30.             PileOfCoinsParent.transform.GetChild(i).position = InitialPos;
    31.             PileOfCoinsParent.transform.GetChild(i).rotation = InitialRotation;
    32.         }
    33.     }
    34.     public void SpawnCoins( int coinsNumber)
    35.     {
    36.         Reset();
    37.         var delay = 1f;
    38.         PileOfCoinsParent.SetActive(true);
    39.         for (int i = 0; i < PileOfCoinsParent.transform.childCount; i++)
    40.         {
    41.             PileOfCoinsParent.transform.GetChild(i).DOScale(endValue: .8f, duration: 0.3f).SetDelay(delay).SetEase(Ease.OutBack);
    42.             PileOfCoinsParent.transform.GetChild(i). GetComponent<RectTransform>().DOAnchorPos(endValue: new Vector2(-386f, 493f), .5f).SetDelay(delay +.5f).SetEase(Ease.OutBack);
    43.             PileOfCoinsParent.transform.GetChild(i).DORotate(Vector3.zero, .5f).SetDelay(delay + .5f).SetEase(Ease.Flash);
    44.             PileOfCoinsParent.transform.GetChild(i).DOScale(endValue: 0f, duration: 0.3f).SetDelay(delay + 1.8f).SetEase(Ease.OutBack);
    45.             delay += 0.1f;
    46.         }
    47.     }
    48. }
     
    Last edited: May 23, 2024
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    838
  3. GameLover90

    GameLover90

    Joined:
    Jan 5, 2021
    Posts:
    13
    if you show me how to do it, i will edit. thanks
     
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    838
    The page I linked shows how to do it. That’s why I linked it.
     
  5. GameLover90

    GameLover90

    Joined:
    Jan 5, 2021
    Posts:
    13
  6. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    4,002
    that was a waste of money, this is really a simple issue to resolve without spending cash
     
    GameLover90 likes this.
  7. GameLover90

    GameLover90

    Joined:
    Jan 5, 2021
    Posts:
    13
    Thanks I just didn't know how :)
     
  8. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,307
    You waited less than three hours before buying this asset and your most recent post before then. If you can't either work the solution out on your own or be patient enough to wait for help, you're going to run into way bigger problems down the line that will either get very expensive very fast or stop you dead in your tracks. On top of that, because this is such a basic problem, you should have posted it in the beginner section, "Getting Started," not "General Discussion.
     
    GameLover90 and MadeFromPolygons like this.
  9. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    4,002
    Next time I suggest waiting at least 1 day, before buying something. I would have happily helped you resolve this issue, but you had already bought something by the time I saw this thread.

    As @Murgilod said, 3 hours is just not a sensible timeframe to wait before buying something, unless you are rich and made of money in which case buy everything you want. But ideally you should be as frugal as possible in game development.

    24 hours to get a resolution to an issue is really the bare minimum if you are posting on a public forum, and that in the future is how long you should wait.
     
    GameLover90 likes this.