Tag Archives: objects

Stopping the player from sticking to the edge of platforms and walls in a 2D Unity game

This tutorial will show you a quick fix for stopping the player from sticking to the edge of platforms and walls in your 2D Unity game.

Next tutorial: Setting up a fall detector

Organising and grouping assets in the hierarchy in Unity

This tutorial explains how to organise and group assets on the hierarchy in your 2D game as well as the parent-child relationship between assets on the hierarchy.

Next tutorial: Introduction to C# scripting and collision detection

Accessing other objects and modifying their components

This tutorial explains how you can write a script in C# that can access other objects and then modify their components. In this example, we will attach a script to the Main Camera and then use that script to access a Cube object and modify it’s position, rotation and scale.

Here is the sample code that is attached to the Main Camera and accessing the Cube object.

using UnityEngine;
using System.Collections;
 
public class moveOtherObject : MonoBehaviour {
 
 // this script accesses another object and modifies its components
 // it can be attached to any other object e.g script and is accessing a Cube
 
 // this is the other game object you want to access
 public GameObject target;
 
 // Use this for initialization
 void Start () {
 target = GameObject.Find ("Cube");
 
 }
 
 // Update is called once per frame
 void Update () {
 target.transform.Translate (0f, 0f, 0.1f);
 target.transform.Rotate (0f, 0f, 1f);
 target.transform.localScale = new Vector3 (1.5f, 1.5f, 1.5f);
 
 }
}

Transforming objects using scripts

This video tutorial will show you how to transform objects (move, rotate, scale) in your Unity game using C# scripts and also how to implement player interaction with the game by allowing players to transform objects using keypresses on their keyboard.

Watch the video below and then scroll down to see the sample code.

Here is some sample code with different object transformations assigned to different keypresses on the keyboard. You can change the transformations and assigned keys although it is a good idea to use the input manager rather than specific keypress detection so that your players have the option of remapping keys to suit their own preferences.

using UnityEngine;
using System.Collections;
 
public class move : MonoBehaviour {
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 if (Input.GetKey (KeyCode.W)) {
 transform.Translate (0f,0f,0.1f);
 }
 if (Input.GetKey (KeyCode.S)) {
 transform.Translate (0f,0f,-0.1f);
 }
 
 if (Input.GetKey (KeyCode.D)) {
 transform.Translate (0.1f,0f,0f);
 }
 if (Input.GetKey (KeyCode.A)) {
 transform.Translate (-0.1f,0f,0f);
 }
 
 if (Input.GetKey (KeyCode.Z)) {
 transform.Rotate (0f,1f,0f);
 }
 
 if (Input.GetKey (KeyCode.X)) {
 transform.Rotate (0f,-1f,0f);
 }
 
 if (Input.GetKey (KeyCode.C)) {
 transform.localScale = new Vector3(1f,1f,1f);
 }
 
 if (Input.GetKey (KeyCode.V)) {
 transform.localScale = new Vector3(5f,5f,5f);
 }
 
 }
}

Note: If you find that the values for x, y and z axes seem to work the opposite to what you expect, it might be because you have the camera at a different angle. For example, if your camera is on the opposite side of your object, then pressing the key that is meant to move the object left might actually move it to the right.

Now try using variables to store the values for speed, eg:

using UnityEngine;
using System.Collections;
 
public class move : MonoBehaviour {
 
 public float turnSpeed = 5.0f;
 public float forwardSpeed = 0.25f;
 
 // Update is called once per frame
 void Update () {
 transform.Rotate(turnSpeed, 0.0f, 0.0f);
 transform.Translate(0.0f, 0.0f, forwardSpeed);
 }
}

Here is another method of moving objects on their own at a steady speed (this example does not include keyboard input).

using UnityEngine;
using System.Collections;
 
public class moveCube : MonoBehaviour {
 
 public float speed = 1.0f;
 
 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
 transform.Translate (Vector3.forward * speed * Time.deltaTime);
 // This will move the cube forward on its own at steady speed
 // Vector3 is for 3D , Vector2 is for 2D
 // You can change Vector3.forward to Vector3.back, Vector3.left, or Vector3.right
 
 
 }
}

Exporting 3D models from Maya into Unity

This tutorial explains how to export detailed 3D models and objects such as characters or buildings made in Maya and import them into a Unity 3D game project. The video also explains how to embed the materials/textures into the exported file.

If you can’t find the FBX Export option when selecting the file type to export as in Maya, then you might need to enable FBX in the plugin manager. This is explained in the video.

Basic lighting and emitting light from objects in Maya

This tutorial explains how to use lights in Maya and also how to emit light from an object such as the Sun or a light bulb (so that it glows). Please note that this video is a basic overview to get you started with some of the many lights and lighting effects you can use in Maya. You can also download printed instructions here.

Adding light decay

You can download the instructions from here if you would like light in your project to lose its intensity or reach over a distance.

Objects, properties and methods in JavaScript

Object-oriented programming is a type of programming where programmers can define not just the data type of a data structure,  but also the different types of operations (or functions) that can be applied to a data structure. Under this model, a data structure is an object which consists of both data and functions. Programmers can create relationships between different objects. For example, objects can be given characteristics that belong to another object.

Programming languages which support object-oriented programming include Java, C++, Python, Visual Basic .Net, Ruby, and of course JavaScript.

In Javascript, an object is something that can have properties and a method is an action that you can perform on an object. A property is something about an object (eg. the length of a string).

Watch the video below first, and then scroll down to view the sample code.

In the example below,  the sentence is the object, the length is the property of the object, and the method is the action used to convert the string to uppercase letters. Look at the sample code below to see how properties and methods are used on objects.

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="utf-8"/>
 <title>JavaScript - Objects, properties and methods</title>
 <script type="text/javascript">
 
 /* Remember that JS is an object-oriented language. 
 An object is something that has properties in JS.
 A method is an action you can perform with an object.
 A property is something about an object (eg. the length of a string).
 
 In the example below, sentence is the object, length is the property of the object and method is the
 action used to convert the string to uppercase letters.
 To work with properties, we use the syntax objectname.propertyname 
 A method will always be followed by brackets eg. objectname.methodname()
 A method will sometimes require parameters.
 */
 
 var sentence = "Hello world";
 // This is an example of using a property
 document.write(sentence.length,"<br/>"); // the <br/> tag is simply used to make a new line here
 // This is an example of using a method.
 document.write(sentence.toUpperCase());
 
 //With document.write , document is the object and write is the method.
 
 </script>
 
</head>
<body>
 
</body>
</html>

Next tutorial: Using the getElementById() method

How to crop objects outside the stage in Adobe Flash CS6

This tutorial explains how to delete or crop content that is outside the stage area in an Adobe Flash CS6 animation. You can watch the video on YouTube here or view it below.

Note: The purpose of this tutorial is to explain how to crop objects outside the stage if you want to clean up any clutter around  your animation. In the example above, you might later on want to make the clouds move using a motion tween. If that is case, then it is not a good idea to crop the clouds.

How to orient a symbol to a motion tween path in Adobe Flash CS6

This video tutorial explains how to orient a symbol to the path when creating a motion tween effect in Adobe Flash CS6. You can watch the video on YouTube by clicking here or view it below.

How to draw your own objects in Adobe Flash CS6

This video tutorial explains how you can draw your own objects or shapes in Adobe Flash CS6 using a range of drawing tools including the pencil, brush, paint bucket, line, and shape tools. You can also watch the video on YouTube here if your prefer.

How to spin objects using a motion tween rotation

This video tutorial explains how to spin or rotate objects by changing the rotation effects on a motion tween. You can spin stationary or moving objects. The video is also available on YouTube here if you’d prefer to watch it there.