Bubble ZOO Escape Anouncement

  Hi folks! I am proud to present: 
Bubble Zoo Escape, my first Android game built using AndEngine 
If you have a time, please download and give feedback!

Unity start guide

Sep 15

Written by: Goran Siric
9/15/2016 7:43 PM  RssIcon

How to move 2D GameObject between two points in specific amount of time?

Here we are assuming that you want to move object which will not be moved by the physics (elevators, ramps, etc..)

One way to do it is to use Vector3.Lerp, which will return vector between startPosition and endPosition, based on the third parameter which needs to be between 0 and 1.  Calculating totalTime and dividing it by SecondsForMove we are getting value from 0 to 1 for Lerp function.

public class SomeScript : MonoBehaviour {
    public Vector3 endPosition;
    public Vector3 startPosition;
    public float SecondsForMove;
    public float totalTime= 0;
 
    void Update ()
    {
        totalTime += Time.deltaTime;
        transform.position = Vector3.Lerp (startPosition, endPosition, totalTime / SecondsForMove);
    }
}

Keep in mind that if you add collider to such types of object, also add Rigidbody 2D component to it, and set "Is Kinematic" to true, to improve ingame physics performance.


How to quit game when the Android Back button is pressed?

In  Unity, android Back button is mapped to Escape key. So, it will work with Escape key too.

Put this code on script attached to Main Camera or whatever  else script you want. 

Note: It will also stop playing your game in Unity editor.

    if (Input.GetKeyDown(KeyCode.Escape))
    {
        #if UNITY_EDITOR
          UnityEditor.EditorApplication.isPlaying = false;
        #else
          Application.Quit();
        #endif
    }
}

How to set startup scene?

In the Build Settings, from the File menu, you can reorder your scenes. Make sure startup is at the top of the list.
The Order in Build Settings doesn't affect the Play Button at all. It is only for when you actually Build/Export your Game.

How to detect finger touch or mouse click on 2d collider?

Here is one working example for detecting left mouse click and any finger touch on any GameObject which have 2d colider attached to it:

First here is simple class which use Physics2D.Raycast for detecting touch on coliders.

using UnityEngine;
using System.Collections;
 
public static class InputController
{
    private static bool buttonDown = false;
    private static RaycastHit2D noHit = new RaycastHit2D();
 
    public static RaycastHit2D checkTouchDown()
    {
        if (!Input.GetMouseButtonDown (0))
            buttonDown = false;
 
        foreach (Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                RaycastHit2D hit = _checkTouchDown(touch.position);
                if (hit.collider != null)
                    return hit;
            }
        }
 
 
        if (Input.GetMouseButtonDown (0) && !buttonDown)
        {
            buttonDown = true;
            RaycastHit2D hit = _checkTouchDown(Input.mousePosition);
            if (hit.collider != null)
                return hit;
        }      
 
        return noHit;
    }
 
    public static RaycastHit2D _checkTouchDown(Vector2 position)
    {
        return Physics2D.Raycast (Camera.main.ScreenToWorldPoint (position), Vector2.zero);
    }
}


Now you can use this class in Update method of any other script. You should check hit.collider property, which will be non null if there is collision with some colider. Here I am checking if my collider (GameObject) tag is "PlayButton", and if it is, then I am destroying play button and loading "next scene"

// Update is called once per frame
void Update ()
{
 
    RaycastHit2D hit = InputController.checkTouchDown ();
    if (hit.collider != null)
    {
        if (hit.collider.tag == "PlayButton")   
        {
            Destroy(hit.transform.gameObject);
            SceneManager.LoadScene("next scene");
        }
    }
 
}



Tags: Unity
Categories:

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

Bubble Zoo Escape

  Hi folks! I am proud to present: 
Bubble Zoo Escape, my first Android game built using AndEngine 
If you have a time, please download and give feedback!

Search Blog

If you found this useful,
you can buy me a coffe :)
By me a coffe through PayPal :)

Featured articles

AndEngine - Textures tips and tricks

What you should know abot textures before get started programming your first game.

GIMP script for creating Android icons at once

Script for creating Android icons for different screen resolutions at once. Icons can be saved using standard Android icons naming conventions and saved in appropriate folders.

Creating Android button with image and text using relative layout

Source code with examples how to use relative layout to create nice buttons with text and images in Android

Android application and the Internet

Tutorial about connecting to the web pages on the Internet from Android application, using both POST and GET web requests.

Recent Entries

Unity start guide
AndEngine - Textures tips and tricks
Calculated variables
Comments about Parking SMS Scheduler
Comments about cities and parking zones
Android "DataEntry ListView" Design Pattern
Android application and the Internet

Recent Comments

Re: Creating Android button with image and text using relative layout
What if, using the drawable left method, i want to increase the distance of the icon from the left limit of the button, making it a bit more central?
Re: Creating Android button with image and text using relative layout
Hi Shameel,try to use ImageView android:scaleType property in xml button layout:

android:scaleType="centerInside"

For more info see: developer.android.com/reference/android/widget/ImageView.ScaleType.html
Re: Creating Android button with image and text using relative layout
Hi bro,

Can u tell me how to set the size of icon according to our requirement in button ?

Actually my icon size is 256x256 and i want 20x20 ???
Re: Creating Android button with image and text using relative layout
Hi dude, i have also find out one good example

How To Create Button

Re: Creating Android button with image and text using relative layout
Hello, I am new for android.I want to move text on image view. how its possible?
Plz help me.Thanks in advance