170 likes | 336 Views
Physics and Special Effects. Chapter 11. Physics Basics. Rigidbody needed to allow physics engine to interact with objects Use Gravity setting needed to imitate gravity Physics material of collider allows for more realistic movement, “bouncy”.
E N D
Physics and Special Effects Chapter 11
Physics Basics • Rigidbody needed to allow physics engine to interact with objects • Use Gravity setting needed to imitate gravity • Physics material of collider allows for more realistic movement, “bouncy”. • Drag necessary to imitate atmosphere for falling objects • Mass matters in collisions
Forces • Constant Force component allows for application of a constant force • Force field is global • Relative force field is with respect to the object • Constraints allows you to limit movement in a particular direction • Torque allows for the object to spin based on force • Angular drag will slow spin
Joints • Hinge Joint component allows for “door-like” object to be added and move like a door • Break torque and Break Force gives limits on the strength of the hinge • The hinge Joint component is removed when the hinge breaks
Combining physics and keyframe animation • Should set the Use Kinematic setting to let physics engine know to check for movement not caused by the engine.
Cloth • One sided object that reacts like cloth • Interacts with the physics engine
Projectiles #pragma strict var projectile : GameObject; // the object to instantiate var speed : float = 20.0; // default speed varactivateRate : float = 0.5; // how often to trigger the action internal varnextActivationTime : float; // target time function Update () { // if the Fire1 button (default is left ctrl) is pressed and the alloted time has passed if (Input.GetButton ("Fire1") && Time.time > nextActivationTime) { nextActivationTime = Time.time + activateRate; // reset the timer Activate(); // do whatever the fire button controls } } function Activate () { // create a clone of the projectile at the location & orientation of the script's parent var clone : GameObject = Instantiate (projectile, transform.position, transform.rotation); // add some force to send the projectile off in its forward direction clone.rigidbody.velocity = transform.TransformDirection(Vector3 (0,0,speed)); // ignore the collider on the object the script is on Physics.IgnoreCollision(clone.collider, transform.collider); }
Collision Detection • Default collision detection is discreet • May need to change to continuous mode detection depending on velocity • Destroy function used to remove object from scene
RockFall • Prefab is instantiated at original coordinates rather than relative to launcher • Multiple prefabs can be put into empty gameObject • Collection of objects can be made into a package
Randomness #pragma strict // Instantiates prefab when any rigid body enters the trigger. // It preserves the prefab's original position and rotation. var rocks : GameObject[]; // create an array to hold the rocks internal vararrayLength : int; // var to hold number of elements in the array var prize : GameObject; // the crystal varpileTime : float = 10.0; // time to let rocks drop varstartTimer : boolean = false; // flag for timer after first rock drop function Start () { arrayLength = rocks.length; // number of elements in the array } function OnTriggerEnter () { // get a random number between 0 and the length of the array varnum = Random.Range(0, arrayLength); //instanciate that element Instantiate (rocks[num]); if(!startTimer) DropPrize (); // start the timer function } function DropPrize () { startTimer = true; // timer running yield new WaitForSeconds(pileTime + 0.5); // allow extra to let rocks settle //activate prize prize.SetActive(true); prize.GetComponent(Interactor).currentState = 1; // manually change its state yield; // wait a frame Destroy(this.gameObject); // terminate the Rock Zone object }
Particle Systems • Particles can be affected by gravity, have different velocities and life spans, and be emitted from a single spot or across a large area. • They can vary in size throughout their lives, or they can start one size and grow or shrink during their lifetimes. • Their opacities can vary, and they can be made additive to produce bright fire-like effects.
Shuriken Basics • Two types of particles • Happen throughout the scene • Instantiated at a point • Prewarm starts particles at beginning of scene • Start delay is delay before particles start • Start lifetime is default life span of individual particles • Start color is color of particles • Gravity multiplier effects direction and speed of particles
Shuriken Basics • Inherit velocity deals with moving emitter • Simulation space can be local or world space • Local space would be used if emitter moves • Play on awake starts emitter as soon as the scene starts • Max particles limits the number of particles in the scene at an time • Particles will stop being emitted until older one die off
Shuriken Modules • Pouring particles • Particle Groups • Allow editing of multiple particle systems at once
Laser Beam #pragma strict varlaser : Transform; // the object with the Line Renderer component var range : int = 30; // the distance to check within varhitLight : Light; // light at the end of the laser varhitParticles : GameObject;// the sparks prefab internal var hit : RaycastHit; // holds some of the properties of the object that is detected with the raycast function Update () { // is right mouse button down? if(Input.GetButton("ML Enable") ) {// if player is looking around // Did we hit anything? if (Physics.Raycast (transform.position, transform.forward, hit, range)) { laser.GetComponent(LineRenderer).SetPosition (1,Vector3(0, 0,hit.distance -0.45));//update end position if (hitParticles && hit.collider.tag == "ActionObject") { // if particles were assigned, instantiate them at the hit point var temp : GameObject = Instantiate(hitParticles, hit.point,Quaternion.FromToRotation(Vector3.up, hit.normal)); Destroy(temp, 0.3); } hitLight.intensity= 5.0; // turn the intensity up hitLight.transform.position= hit.point; // move the light to the hit point } } else { //print ("There's nothing directly ahead"); laser.GetComponent(LineRenderer).SetPosition (1,Vector3(0, 0, 0)); // shorten the laser hitLight.intensity= 0.0; // turn the intensity off if there was no hit } }
UV Animator #pragma strict // Script to scroll main texture and bump based on time varmaterialIndex : int = 0; // in case the objects has more than one material varanimateUV = true; // flag for option to scroll texture var scrollSpeedU1 = 0.0; // variables to scroll texture var scrollSpeedV1 = 0.0; varanimateBump = false; // flag for option to scroll bump texture var scrollSpeedU2 = 0.0; // variables to scroll bump texture var scrollSpeedV2 = 0.0; function Start () { //print ("shininess " + renderer.materials[materialIndex].HasProperty("_Shininess")); //print ("parallax " + renderer.materials[materialIndex].HasProperty("_Parallax")); } function FixedUpdate () { // texture offset variables var offsetU1 = Time.time * -scrollSpeedU1; var offsetV1 = Time.time * -scrollSpeedV1; // bump texture offset variables var offsetU2 = Time.time * -scrollSpeedU2; var offsetV2 = Time.time * -scrollSpeedV2; if (animateUV) { // if the flag to animate the texture is true... renderer.materials[materialIndex].SetTextureOffset ("_MainTex",Vector2(offsetU1,offsetV1)); } if (animateBump) { // if the flag to animate the bump texture is true... renderer.materials[materialIndex].SetTextureOffset ("_BumpMap", Vector2(offsetU2,offsetV2)); } }
Summary In this chapter, you imported several new Action objects that you will eventually use in the game. While setting up the materials for the new objects, you took a trip to Unity’s asset store and took advantage of a free asset package, the GemPack. It contained a shader that uses a couple of cube maps for reflection and refraction. Next, you were introduced to physics, starting with the Rigidbody component. Along with it, you experimented with physic Materials that you assigned to the colliders, hinges, and forces. You had a first look at cloth and were able to see how it could interact with other objects, both animated and static. After a few tests, you used the Rigidbody’s Velocity parameter to create a very simple projectile. You learned to limit the number and frequency of projectiles available to the player by scripting a little timer. To incorporate physics into the game, you created a rockfall that instantiated random rocks from an array. After adding a collider, a rigid body, and some scripts to control them, you used a rock to make your first UnityPackage. At a specified time, you then instantiated an action object, the Crystal, so that it could drop on top of the randomly generated rock pile. You then took advantage of Unity’s legacy particle system to take a quick look at several useful prefabs. Delving into Unity’s Shuriken particle system, you took advantage of several of the imported textures to set up a few different particle systems that will come in handy later on in the game. With the fire effect, you learned how you could use multiple Shuriken particle systems together as a Particle Effect. You also found that a simple point light could enhance particle effects. Finally, you made a laser beam with the help of the Line Renderer and Physics.Raycast. With the information returned by Raycast.hit, you were able to control the length of the laser beam and instantiate particles and a light at its hit point. You created a useful little script that animates a material’s U and V Offset and applied it to the laser beam to give it a more dynamic look.