Friday, December 31, 2021

Check animator is playing or finshed


https://answers.unity.com/questions/362629/how-can-i-check-if-an-animation-is-being-played-or.html?page=1&pageSize=5&sort=votes


 just send your animation name in CurrentAnimation you want to check.

  1. public IEnumerator CheckAnimationCompleted(string CurrentAnim, Action Oncomplete)
  2. {
  3. while (!Animator.GetCurrentAnimatorStateInfo(0).IsName(CurrentAnim))
  4. yield return null;
  5. if (Oncomplete != null)
  6. Oncomplete();
  7. }

calling coroutine

  1. StartCoroutine(CheckAnimationCompleted("Shoot", () =>
  2. {
  3. Animator.SetBool("Shoot", false);
  4. // Your any code
  5. }
  6. ));

Wednesday, December 1, 2021

Check gameobject is visible on screen

 


Answer by Taylor-Libonati 

Here's the method I used with success:

Convert the position of the object into viewport space. If the viewport space is within 0-1 you are in the cameras frustum. This method also allows you to add a margin amount if you want, so that objects turn on when they are almost in view.

  1. Vector3 screenPoint = playerHead.leftCamera.WorldToViewportPoint(targetPoint.position);
  2. bool onScreen = screenPoint.z > 0 && screenPoint.x > 0 && screenPoint.x < 1 && screenPoint.y > 0 && screenPoint.y < 1;

Instantiate Photon object without using Resources folder

 https://forum.unity.com/threads/solved-photon-instantiating-prefabs-without-putting-them-in-a-resources-folder.293853/


PUN 2 is using a PrefabPool to get a GameObject instance. You can write your own but you can also very easily fill the existing pool of type DefaultPool.

The attached MonoBehaviour has a list of "prefabs", which can be edited in the Inspector. This reference means that the assets don't have to be in the Resources folder. In Start(), the list is used to "fill" the DefaultPool. That's enough.

Code (CSharp):
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using Photon.Pun;
  4.  
  5.  
  6. public class PreparePool : MonoBehaviour
  7. {
  8.     public List<GameObject> Prefabs;
  9.  
  10.     void Start ()
  11.     {
  12.         DefaultPool pool = PhotonNetwork.PrefabPool as DefaultPool;
  13.         if (pool != null && this.Prefabs != null)
  14.         {
  15.             foreach (GameObject prefab in this.Prefabs)
  16.             {
  17.                 pool.ResourceCache.Add(prefab.name, prefab);
  18.             }
  19.         }
  20.     }
  21. }

Map circle coord to rectanglar coord

 https://stackoverflow.com/questions/13211595/how-can-i-convert-coordinates-on-a-circle-to-coordinates-on-a-square


See Mapping a Square to a Circle. There's also a nice visualization for the mapping. You get:

xCircle = xSquare * sqrt(1 - 0.5*ySquare^2)
yCircle = ySquare * sqrt(1 - 0.5*xSquare^2)