lundi 14 novembre 2011

Polyhedron Shower

Après avoir créé le code pour l’instanciation d'un cube, je n'ai pas pu m'arrêter et j'ai continué à ajouter du code. Voici le résultat:
-->

  • RandomObjectCreator.cs: crée les polygones à partir d'un tableau de Transform, les places de manières aléatoires et leurs donnent une vitesse et une rotation aléatoire.
  • RandomColorMaterial.cs: change la couleur et la transparence du matériel. Les belles couleurs viennent de se simple script!
  • Timer.cs: C'est une classe utilitaire qui indique si le délai est écoulé en appelant la fonction trigger.
  • PointLightAtCenter.cs: ce script ajoute une lampe au centre de mes polyèdres. Ce sont ces petites lampes qui ajoutent l'ambiance et font ressortir les couleurs.
  • BounceBack.cs: ce script inverse la vitesse des polyèdres lorsqu'il dépasse une certaine distance du centre.
RandomObjectCreator.cs

using UnityEngine;
using System.Collections;

/**
 * Choose randomly a polyhedron and instantiate it at a random position with
 * a random rotation. After that, add a RigidBody and assign a random force and
 * a random torque to move and rotate the polyhedron.
 *
 * @author Mentalogicus
 * @date 11-2011
 * 
 * Copyright (C) 2011, Mentalogicus
 * All rights reserved
 * 
 */
public class RandomObjectCreator : MonoBehaviour 
{
 
 
 public Transform[] _prefab;
 public float _force = 10;
 public float _radius = 10;
 
 // Use this for initialization
 void Start () 
 {
  
 }
 
 // Update is called once per frame
 void Update () 
 {
  
  if ( Input.GetKey("space") )
  {
   //Create a random position.
   //The insideUnitSphere return a random position inside a sphere of radius 1.
   Vector3 rndPos = Random.insideUnitSphere * 10;
   
   //Create a random rotation.
   Quaternion rndRotation = Random.rotation;
   
   //Choose randomly between the array of object
   int choice = (int) Mathf.Floor(Random.Range( 0.0f, _prefab.Length -.0000001f ) );
   
   //Instantiate a new object at a random position with a random rotation.
   Transform newGameObj = Instantiate( _prefab[choice], rndPos, rndRotation) as Transform; 
   
   //Rename the game object.
   newGameObj.name = "Allo";
   
   //Add a rigid body to the object.
   newGameObj.gameObject.AddComponent();
   
   //Remove the gravity.
   newGameObj.gameObject.rigidbody.useGravity = false;
   
   //Change the mass of the object to 1.
   newGameObj.gameObject.rigidbody.mass = 1.0f;
   
   //Add a random impulse to the rigidbody.
   newGameObj.gameObject.rigidbody.AddForce(new Vector3( Random.Range(-_force,_force), 
                                                         Random.Range(-_force,_force), 
                                                         Random.Range(-_force,_force) ), 
                                                 ForceMode.Impulse);
   
   //We add a random torque to the rigidbody to make him rotate!!!
   newGameObj.gameObject.rigidbody.AddTorque(new Vector3( Random.Range(-_force,_force), 
                                                         Random.Range(-_force,_force), 
                                                         Random.Range(-_force,_force) ), 
                                                 ForceMode.Impulse);
  }
  else if ( Input.GetKey("q") )
  {
   Application.Quit();
  }
  else if ( Input.GetKeyDown("return") )
  {
   GameObject[] objs = GameObject.FindGameObjectsWithTag ("Block");
   foreach ( GameObject obj in objs)
   {
    Destroy(obj);
   }
  }
 }

}

Licence Creative Commons
RandomObjectCreator.cs de Mentalogicus est mis à disposition selon les termes de la licence Creative Commons Paternité - Pas de Modification 3.0 non transposé.  

RandomColorMaterial.cs

using UnityEngine;
using System.Collections;

/**
 * Randomly assign a color and an alpha to the material of the object.
 * 
 *
 * @author Mentalogicus
 * @date 11-2011
 * 
 * Copyright (C) 2011, Mentalogicus
 * All rights reserved
 * 
 */
public class RandomColorMaterial : MonoBehaviour
{

 
 // Use this for initialization
 void Start ()
 {
  
  Shader shader1 = Shader.Find("Transparent/Diffuse");
  
  this.renderer.material.shader = shader1;
  
  //Set the main color of the material
  this.renderer.material.color = CreateRandomColor();
  
 }
 
 private Color CreateRandomColor()
 {
  float r = Random.value;
  float g = Random.value;
  float b = Random.value;
  float a = Random.Range(.4f,1f);
  
  return new Color( r, g, b, a );
 }
}
Licence Creative Commons
RandomColorMaterial.cs de Mentalogicus est mis à disposition selon les termes de la licence Creative Commons Paternité - Pas de Modification 3.0 non transposé.

Timer.cs

using System;
using UnityEngine;

/**
 * Timer with a delay. When the delay is over by, the trigger properties will
 * return true.
 *
 * @author Mentalogicus
 * @date 11-2011
 * 
 * Copyright (C) 2011, Mentalogicus
 * All rights reserved
 * 
 */
namespace Utils
{
 public class Timer
 {
  private float _timer;
  private float _delay;
  
  
  public void Start(  )
  {
   this.Reset();
  }
  
  public void Start( float delay )
  {
   _delay = delay;
   this.Reset();
  }
  
  public float time
  {
   set { _timer = value; } 
  }
  
  public float delay
  {
   get { return _delay; }
   set { _delay = value; }
  }
  
  public void Reset()
  {
   _timer = Time.time; 
  }
  
  public bool overBy
  {
   get { return (Time.time - _timer) >= _delay; }
  }
  
  public bool underBy
  {
   get { return (Time.time - _timer) < _delay; }
  }
  
  /**
   * When called, this properties return if the timer is overBy or underBy and
   * reset the timer.
   */
  public bool trigger
  {
   get
   {
    if ( overBy )
    {
     Reset();
     return true; 
    }
    else
    {
     return false;
    }
   }
  }
  
 } //End class
} //End namespace


Licence Creative Commons
Timer.cs de Mentalogicus est mis à disposition selon les termes de la licence Creative Commons Paternité - Pas de Modification 3.0 non transposé.
 
PointLightAtCenter.cs

using UnityEngine;
using System.Collections;

/**
 * Add a point like in the center of an object.
 *
 * @author Mentalogicus
 * @date 11-2011
 * 
 * Copyright (C) 2011, Mentalogicus
 * All rights reserved
 * 
 */
public class PointLightAtCenter : MonoBehaviour
{
 public float _range = 5f;
 
 // Use this for initialization
 void Start ()
 {
  //Add a point light in the center of the object.
  Light spot = this.gameObject.AddComponent();
  spot.type = LightType.Point;
  spot.range = _range;
  spot.intensity = 2;
 }

}


Licence Creative Commons
PointLightAtCenter.cs de Mentalogicus est mis à disposition selon les termes de la licence Creative Commons Paternité - Pas de Modification 3.0 non transposé.
 
BounceBack.cs
 
using UnityEngine;
using System.Collections;
using Utils;

/**
 * Inverse the velocity of an object that is at a distance greater
 * than the specified radius.
 *
 * @author Mentalogicus
 * @date 11-2011
 * 
 * Copyright (C) 2011, Mentalogicus
 * All rights reserved
 * 
 */
public class BounceBack : MonoBehaviour 
{
 
 public float _radius = 50;
 
 private Timer _timeToReturnBack = new Timer( );
 
 void Start()
 {
  _timeToReturnBack.Start( .2f );
 }
 
 // Update is called once per frame
 void Update () 
 {
  if ( this.transform.position.x * this.transform.position.x +
       this.transform.position.y * this.transform.position.y +
       this.transform.position.z * this.transform.position.z > _radius * _radius &&
       _timeToReturnBack.trigger )
  {
   this.rigidbody.velocity = -this.rigidbody.velocity;
  }
 }
}

Licence Creative Commons
BounceBack.cs de Mentalogicus est mis à disposition selon les termes de la licence Creative Commons Paternité - Pas de Modification 3.0 non transposé.

Aucun commentaire:

Enregistrer un commentaire