Download presentation
Presentation is loading. Please wait.
Published byΛωΐς Ιωάννου Modified over 5 years ago
1
Deeper into the Depths: Casting, Scope, Gizmos, Layers, Self-Destruct.
Week 8 Deeper into the Depths: Casting, Scope, Gizmos, Layers, Self-Destruct.
2
Contents 13 Slides Destroy after Duration Raycast || Linecast & Gizmos
Layer Transform.position Script to script communication Scope Awake(); Sprites FSM
3
Destroy after duration
Destroy is a built- in method. It removes the object from the scene and from active memory. But what if you want to set something to SELF DESTRUCT! You have that option: Destroy (gameObject, 1.5f); Now our arguments are what (object) and when (float) !
4
Raycast & Linecast & Gizmos
Imagine a beam, shooting out of your belly button for a predefined amount of distance. It moves with you, and it notifies you when you hit something, or how close you are to it. Essentially, RayCast and Linecast are the same. * RAYCAST has us pick an origin, a direction, and how far out we want the ray to go. LINECAST has us pick a start and end point. Linecast: Grounded = Physics2D.Linecast (LineStart.position, LineEnd.position, 1 << LayerMask.NameToLayer("Game")); Bool = Physics2D.LineCast( start point, end point, if you want to confine your search to a layer (“NameOfLayer”)); You can turn on GIZMOS in your game or scene view (the icons that represent locations, such as light, sound and collision)use special Debug options to have your line show up on screen for debugging purposes: Debug.DrawLine (LineStart.position, LineEnd.position, Color.magenta ); *I like to borrow transform.position from things, so I prefer Linecast.
5
Raycast & Linecast cont.
Raycast Parameters include: Origin : The starting point of the ray, in world coordinates. If it is moving, You can assign the coordinates of another object here Direction: The direction you want the ray to point in. Distance: The length outward from the origin you want the Raycast to reach. Optionally: Hit Info: The thing that hits the raycast. Like Collider, you need a temporary variable to store this in. EX: out HitThing or out Paratrooper if (Physics.Raycast (transform.position, Vector3.up, 10, out HitThing)) { Do these Instructions; }
6
Layers Layers are similar in concept to TAGS, but instead of applying a name to a group, we can apply or restrict things from happening to a bunch of objects. Think of layers as photoshop layers or stacked animation cellophane transparencies. One may contain the character Under may be the background Over may be some spiffy effects, like fog. Although we see everything, we can choose to only effect the things on a certain sheet or layer, and the rest remain unaffected. Layers can be used to Render/not render certain parts of a level, or detect objects only on a certain area with raycasting (and thus, ignore the rest) In example, in our 2D game, we will use a Linecast to look for the player, but we don’t want to run code every time we hit anything with our line, instead, we will only detect things on the player layer.
7
Transform.Position As mentioned, I enjoy stealing X,Y and Z position information from other objects. This can be handy for moving or parented objects. You can use declare a variable with the type Transform. In this variable, you will place an object, but you will be accessing it’s current position, scale and rotation information from it. Using this for LineCast makes positioning your origin and end both easy, and visual. Unlike RayCast, you can also easily work with funky angles. Examples: public Transform LineStart, LineEnd; // Variable Declaration of a transform datatype! ** Gun.transform.position // Borrows the gun’s position at each frame transform.position.y // Borrows only the Y value of the current position! **Look! More shorthand! Declare multiple variables on a single line with commas!
8
Script to Script Conversations
Scripts can get information from each other, call functions AND change each other’s variables. In order for a script to modify a variable on another script, the variable must be PUBLIC. In example: other.collider.GetComponent<ENEMY>().IsAlive = false; other.collider.GetComponent says that we are looking for a component attached to the collider of the thing we hit. <ENEMY> is the name of the script we are addressing. () always good to have a box for information. .IsAlive is dot-syntax. We are looking for the script, and a component of the script called IsAlive (which is the variable we want to change) And lastly, we are assign the value false to the variable IsAlive on the script ENEMY which is attached to the component of the thing we hit. ** You can also make a REFERENCE of a script in another script, by holding the script object in a variable, to make it faster to access.
9
Scope Sometimes you just need a quick variable to use for a line, then throw away. Scope is how much code can ACCESS the or use that variable. You can declare a variable IN a function line by writing a regular variable declaration. This variable, however, is ONLY usable by that function and must be declared before it is used. No one else can get to it. Since no one else can use it or look at it, you omit the privacy from it. Example: void Stuff(){ int StuffNumber = Random.range (0, 51); } Stuff number will not exist outside the Stuff function.
10
Awake Awake is another built in Unity Method.
Awake runs once, before start. (When the script is being loaded) If you need to pass information back and forth in your start function, you can get references to other scripts set up before that. Exciting!!!
11
Sprites and 2D Unity3d Acorns! Because!
SPRITES are 2d images (they are invisible along the edge) Sprites are assembled into “Sprite sheets” , or textures with many images on them to save memory, then are sliced up within the engine for use. SPRITE ANIMATION can be done by drawing each individual frame, or by assembling pictures together and creating Keyframes for moving parts, similar to Flash. BILLBOARDING turns sprites to always face the camera, so you never see the edge. Unity is a 3d engine, even when it is in 2D mode. You can use Z-Depth to your advantage for moving things closer or farther to the camera, sorting things on top of others, or combine 2d and 3d elements in your game. You can toggle between the 2d and 3d spaces, at the top of your scene window. Unity has a separate set of functions for dealing with 2D physics and colliders.
12
FSM FSM or Finite State Machine, is the term for the series of conditions and executions used in creating artificial intelligence in games. FSMs are generally a series of branch paths, as defined by IF statements, that switch the Bot (enemy) from state to state (different types of reactions. This is likely on your final.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.