Snippets

Explosion

Appliquer une explosion à des objets :

public float ExplosionForce = 10000;
public float ExplosionRadius = 5;
public Transform origin = null;

void explosion(){
	Collider[] targets = Physics.OverlapSphere(origin, 5);
	foreach (Collider nearObject in targets)
	{
	  if (nearObject.CompareTag("Player")) // exclut un tag de la collision
		continue;

	  Rigidbody rb = nearObject.GetComponent<Rigidbody>();

	  if (rb != null)
	  {
		rb.AddExplosionForce(ExplosionForce, origin, ExplosionRadius);
	  }
	}
}

Raycast

Affiche un laser sur le raycast

  public float rayLength = 200;
  public LayerMask layers;
  LineRenderer lineRenderer;
  private RaycastHit hit;

  void Awake()
  {
    lineRenderer = GetComponent<LineRenderer>();
  }

  private void Update()
  {
    BetterRaycast();

  }

  // function qui execute le raycast
  void BetterRaycast()
  {
    lineRenderer.SetPosition(0, transform.position);
    if (Physics.Raycast(transform.position, -transform.up, out hit, rayLength, layers))
    {
      lineRenderer.SetPosition(1, hit.point);
    }
    else
    {
      lineRenderer.SetPosition(1, transform.TransformPoint(Vector3.down * 20));
    }
  }

Graphique

Créer un graphique pour voir visuellement les changements de valeur d’une variable

public AnimationCurve plot = new AnimationCurve();

void Update(){
    plot.AddKeey(Time.realtimeSinceStartup, value);
}

ObjectMinder

Ce script permet d’enregistrer les modifications d’un objet durant le mode Play pour les sauvegarder dans l’editor.

//Made by Filmstorm - Author: Kieren Hovasapian 
//Object Minder - Keeps In-Game Object Changes in Sync with Editor

using System.Collections;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[ExecuteInEditMode]
public class ObjectMinder : MonoBehaviour
{

  [Header("State of Play")]
  public bool InEditor;
  public bool InGame;

  [Header("Controls")]
  public bool setValuesToLastSave;
  public string tagToSave;

  private GameObject[] FoundObjectsInGame;
  private List<GameObject> InGameObjects;


  string savePath;

  private void Awake()
  {
    InGameObjects = new List<GameObject>();
    //Setup save path 
    savePath = Path.Combine(Application.persistentDataPath, "saveFile");
  }
  //Save our data from the scenes objects
  void Save()
  {
    using (var writer = new BinaryWriter(File.Open(savePath, FileMode.Create)))
    {
      for (int i = 0; i < InGameObjects.Count; i++)
      {
        Transform t = InGameObjects[i].transform;
        writer.Write(t.localPosition.x);
        writer.Write(t.localPosition.y);
        writer.Write(t.localPosition.z);
        writer.Write(t.localRotation.w);
        writer.Write(t.localRotation.x);
        writer.Write(t.localRotation.y);
        writer.Write(t.localRotation.z);
        writer.Write(t.localScale.x);
        writer.Write(t.localScale.y);
        writer.Write(t.localScale.z);
      }
    }
    //Debug.Log("I tried to save!");
  }
  //Load specific data we saved previously
  void Load()
  {
    using (var reader = new BinaryReader(File.Open(savePath, FileMode.Open)))
    {
      for (int i = 0; i < InGameObjects.Count; i++)
      {
        Vector3 p;
        Quaternion r;
        Vector3 s;
        p.x = reader.ReadSingle();
        p.y = reader.ReadSingle();
        p.z = reader.ReadSingle();
        r.w = reader.ReadSingle();
        r.x = reader.ReadSingle();
        r.y = reader.ReadSingle();
        r.z = reader.ReadSingle();
        s.x = reader.ReadSingle();
        s.y = reader.ReadSingle();
        s.z = reader.ReadSingle();
        //set position from load data
        InGameObjects[i].transform.localPosition = p;
        InGameObjects[i].transform.localRotation = r;
        InGameObjects[i].transform.localScale = s;
        if (i == InGameObjects.Count - 1)
        {
          //Last operation now we break 
          setValuesToLastSave = false;
        }

      }
    }
  }
  // Use this for initialization
  void Start()
  {
    //Check if the game is in play or in editor mode
    if (Application.isPlaying)
    {
      //Debug.Log("Running in play mode");
      InEditor = false;
      InGame = true;
      setValuesToLastSave = true;
    }
    else
    {
      //Debug.Log("In editor");
      InEditor = true;
      InGame = false;
      setValuesToLastSave = true;
    }
  }
  // Update is called once per frame
  void Update()
  {
    //If in game -- find all objects and store their transforms
    if (tagToSave != null)
    {
      FoundObjectsInGame = GameObject.FindGameObjectsWithTag(tagToSave);
      foreach (GameObject go in FoundObjectsInGame)
      {
        if (go.activeInHierarchy)
        {
          if (!InGameObjects.Contains(go))
            //Save Gameobject to List?
            InGameObjects.Add(go);
        }
      }
      //If in editor run the updated loader
      if (InEditor)
      {
        if (setValuesToLastSave == true)
        {
          Load();
        }
      }
    }
  }

  //When we stop playing save object locations
  private void OnApplicationQuit()
  {
    Save();
  }
}

Code permettant de créer des menus circulaires facilement.
source: https://gist.github.com/KenneyNL/62a9e986b55c6b12796d25ea74425718

public float radius = 100.0f;
	public float offset = 0.0f;
	
	void Start(){
		
		for(int i = 0; i < transform.childCount; i++){
			
			float angle = i * (Mathf.PI * 2f) / transform.childCount;
			transform.GetChild(i).localPosition = new Vector3(Mathf.Cos(offset + angle) * radius, Mathf.Sin(offset + angle) * radius, 0);
			
		}
		
	}

Exécution avec un intervalle

Dans certaines situations, au sein des fonctions Update, il peut parfois être intéressant de limiter la fréquence d’exécution de certaines parties du code, relativement couteuses en temps. 

    private int interval = 3;

    private void Update()
    {
        if(Time.frameCount % interval == 0)
        {
            // vos codes
        }
    }