e-mail: calonpintar@gmail.com

intersection test between two objects in unity



this C# script is to detect collision between two objects. let's say there is a player and a enemy, add player & enemy with collider, add the script to the enemy, and then do something..in this case, enemy will be destoyed.

using UnityEngine;
using System.Collections;
public class col : MonoBehaviour {
 public float collisionRadius = 1.1f;
 private GameObject _player;

 void Start() {
     _player = GameObject.FindGameObjectWithTag("Player");
 }

 void Update() {
     Vector3 playerPoint = _player.collider.ClosestPointOnBounds(transform.position);
     float playerRadius = Vector3.Distance(_player.transform.position, playerPoint);

     if (Vector3.Distance(transform.position, _player.transform.position) <= collisionRadius + playerRadius) {
         // Do something!
Debug.Log ("Hello");
         GameObject.Destroy(gameObject);
     }
 }
}

0 comments:

C# make application inside application

create a win 32 form, add a panel named panel1 and use this code. this will not work on directx calls. form, namespace, and panel are depends on your project's name


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
        public Form1()
        {
            InitializeComponent();
        }

        private void LoadApplication(string path, IntPtr handle)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            int timeout = 10 * 1000;     // Timeout value (10s) in case we want to cancel the task if it's taking too long.

            Process p = Process.Start(path);
            while (p.MainWindowHandle == IntPtr.Zero)
            {
                System.Threading.Thread.Sleep(10);
                p.Refresh();

                if (sw.ElapsedMilliseconds > timeout)
                {
                    sw.Stop();
                    return;
                }
            }

            SetParent(p.MainWindowHandle, handle);      // Set the process parent window to the window we want
            SetWindowPos(p.MainWindowHandle, 0, 0, 0, 0, 0, 0x0001 | 0x0040);       // Place the window in the top left of the parent window without resizing it
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            LoadApplication(@"notepad.exe", this.Handle);
        }
    }
}

0 comments:

Unity and Microsoft Kinect SDK

Here i try to mirror a wiki posting from Carnegie Mellon University about integrating Kinect in Unity, the original page is http://wiki.etc.cmu.edu/unity3d/index.php/Microsoft_Kinect_-_Microsoft_SDK as usual, i try to make backup link for the files, in case they are go down.

Here are the links:

- Unity wrapper
http://wiki.etc.cmu.edu/unity3d/images/a/a4/Kinect1.7Wrapper.unitypackage.zip (original)
https://www.mediafire.com/?v5tuwtg3ssma9h7 (my mirror)

- Unity sample
http://www.mediafire.com/download/b91bh5h3l0970bj/Kinect1.7UnitySample.zip (original)
http://wiki.etc.cmu.edu/unity3d/images/6/6e/Kinect1.7UnityWrapper.zip (my mirror)

Open it on Unity, hook a Kinect, and press play. The C# codes are inside.

I'm using:
- Kinect for Xbox (Version 1, with shiny xbox 360 logo on it - the oldest Kinect though)
- Microsoft Kinect SDK 1.8
- Microsoft Kinect Developer Toolkit 1.8
- Microsoft Kinect Runtime
- Unity 4.2 Pro

All credits goes to etc.cmu.edu

If you don't have Microsoft stuff installed on your PC:
http://www.microsoft.com/en-us/kinectforwindows/develop/developer-downloads.aspx

or you can get from my mirror:
Microsoft Kinect SDK 1.8 (227 MB)
Microsoft Kinect Runtime 1.8 (113 MB)
Microsfot Kinect Developer Toolkit 1.8 (384 MB)

0 comments: