Unity start guide

by Goran Siric on Thursday, September 15, 2016 9:43 AM

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");
}
}
}



Author
Goran Siric

Blog about programming



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

By me a coffe through PayPal :)


Featured articles

Integrating ChatGPT shared links with Sourcetree

We'll guide you through the process of incorporating ChatGPT shared links into your commit messages.

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.