U3D Learning-developing 2D games with Unity3d (top)

Source: Internet
Author: User


Welcome to Unity Learning,Unity training ,UnityEnterprise TrainingEducation Area, there are manyu3d Resources,u3d Training Video,u3d Tutorials,U3d Frequently Asked questions,u3d Project Source code, the "Dog Planing Learning Network" Unity's Ultimate Academy, dedicated to building the industry unity3d Training , learning the first brand.

1. Create a Unity3d project
2. Set camera to Orthographic projection
The output of the game is achieved by the scene observed by the camera, which renders the scene to a 2D computer screen with two different projection modes: Perspective projection and orthographic projection, which by default is perspective projection.
Perspective projection
  

Orthogonal projection
  

3. Adding a related object model
Move the camera object, light to the following effect
  

Add game object move out of border control
Using Unityengine;
Using System.Collections;
public class Player:monobehaviour {
public float playerspeed;
Use this for initialization
void Start () {
}
Update is called once per frame
void Update () {
float Amttomove = Input.getaxis ("horizontal") *time.deltatime*playerspeed;
GameObject.transform.Translate (Vector3.right*amttomove);
if (transform.position.x-7.5) {
If the block moves beyond the right side of the game form, the box will enter from the left window
Transform.position = new Vector3 (5.25F,TRANSFORM.POSITION.Y,TRANSFORM.POSITION.Z);
}
if (transform.position.x5.25) {
If the block moves beyond the right side of the game form, the box will enter from the left window
Transform.position = new Vector3 ( -7.5F,TRANSFORM.POSITION.Y,TRANSFORM.POSITION.Z);
}
}
}
4. Creating shells [Create-capsule]
  

Adjust the size of the shells as needed
Select the shell model, stand-alone Component menu, select Physics-rigidbody, and set the shells as rigid bodies so that collision detection can be achieved later.
Add a move action script for shells
Using Unityengine;
Using System.Collections;
public class Bullettile:monobehaviour {
public float bulletspeed;
Private Transform Mytransform;
Use this for initialization
void Start () {
Mytransform = Gameobject.transform;
}
Update is called once per frame
void Update () {
Define the projectile movement speed
float Amttomove = bulletspeed * time.deltatime;
To move the shells vertically upward.
Mytransform. Translate (Vector3.up * amttomove);
Destroy shells if the shells move beyond the game scene
if (mytransform.position.y5.15) {
Destroy (This.gameobject); Destroys the current object
}
}
}
Object Reuse
You can reuse a model object by creating a Prefab object in project and then dragging the object model you want to reuse onto the Prefab object.
The implementation of the firing bullets
To create a prefab reusable object instance in the player script
Using Unityengine;
Using System.Collections;
public class Player:monobehaviour {
public float playerspeed;
Public Gameobject Bulletprefab;
Use this for initialization
void Start () {
}
Update is called once per frame
void Update () {
float Amttomove = Input.getaxis ("horizontal") *time.deltatime*playerspeed;
GameObject.transform.Translate (Vector3.right*amttomove);
if (transform.position.x-7.5) {
If the block moves beyond the right side of the game form, the box will enter from the left window
Transform.position = new Vector3 (5.25F,TRANSFORM.POSITION.Y,TRANSFORM.POSITION.Z);
}
if (transform.position.x5.25) {
If the block moves beyond the right side of the game form, the box will enter from the left window
Transform.position = new Vector3 ( -7.5F,TRANSFORM.POSITION.Y,TRANSFORM.POSITION.Z);
}
Get the transmitter position directly above the emitter
Vector3 position = new Vector3 (transform.position.x,transform.position.y+transform.localscale.y/2.0f, TRANSFORM.POSITION.Z);
Press the SPACEBAR to launch the bullets.
if (Input.getkeydown ("space")) {
Instantiate a Projectile object
Instantiate (bulletprefab,position,quaternion.identity);
}
}
}
  

Dragging represents an attribute instantiation, because our script defines the public property, where we can manually assign a value to the public property, at which point the program runs, with the left and right keys to control the movement of the emitter, with space fired bullets.
  

Application Examples:
Adding and controlling sound in the game
Supported sound files: *.aiff,*.wav,*.mp3,*.ogg
. AIFF
Convert to uncompressed audio import, which is best for short-tone effects. You can compress on demand in the editor.
. Wav
Convert to uncompressed audio import, which is best for short-tone effects. You can compress on demand in the editor
. MP3
Convert to OGG format to import, most suitable for longer music tracks.
. OGG
Compressed audio format (not compatible with iphone devices and some Android devices), best suited for longer music tracks.
Add a Sound control button,
void Ongui () {
Gui. button (new Rect (10,10,100,35), "play Music");
Gui. button (new Rect (10,60,100,35), "pause playback");
Gui. button (new Rect (10,110,100,35), "Stop Music");
}
  

Add an event to a button
void Ongui () {
if (GUI. button (new Rect (10,10,100,35), "Play Music")) {
GameObject.audio.Play ();
}
if (GUI. button (new Rect (10,60,100,35), "pause playback")) {
GameObject.audio.Pause ().
}
if (GUI. button (new Rect (10,110,100,35), "Stop Music")) {
GameObject.audio.Stop ();
}
}
5. Add sound for firing shells
Select Bulletprefab, Component-audio-audionsource in the standalone form
  

  
For more information, please visit the "Dog Planing Learning Network" Unity Ultimate Academy Http://edu.gopedu.com

Statement: This document comes from the "Dog Planing Learning Network" Community-unity Extreme College, is a self-published Unity3d study articles, if anything violates your relevant interests, please communicate with the official, we will deal with the real-time.


U3D Learning-developing 2D games with Unity3d (top)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.