Sunday, December 2, 2012

Unity Detect Collision script

So let say you want to have something happen if collision detected, this is how you do it

1. Create a new script let say "collide"
2. Open your script , put

Function OnCollisionEnter()
{
    Debug.log("Collision detected");
}


However, let say you only want to do something if it collide with specific item

Function OnCollisionEnter( haha : Collision)
{
      if (haha.gameobject.name == "player")
      {
                  Debug.log("Collision detected on the player");
      }
      else if(haha.gameobject.name == "ball")
      {
                  Debug.log("Collision detected on the ball");
      }

}


then you just drag your script to your object that you wanna detect collision

However, this only work on Cube with rigid body

What if, you want the collision detect on your character controller and maybe you want to push the cube or ball around with your main character?

then below is the script that you should create and attach it to your main character



// this script pushes all rigidbodies that the character touches
var pushPower = 2.0;
function OnControllerColliderHit (hit : ControllerColliderHit) {
    var body : Rigidbody = hit.collider.attachedRigidbody;
    // no rigidbody
    if (body == null || body.isKinematic)
        return;
        
    // We dont want to push objects below us
    if (hit.moveDirection.y < -0.3) 
        return;
    
    // Calculate push direction from move direction, 
    // we only push objects to the sides never up and down
    var pushDir : Vector3 = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);

    // If you know how fast your character is trying to move,
    // then you can also multiply the push velocity by that.
    
    // Apply the push
    body.velocity = pushDir * pushPower;
    
    if(hit.gameObject.name == "mycube")
    {
    Debug.Log("pushing cube");
    }
    else if(hit.gameObject.name== "myball")
    {
        Debug.Log("pushing ball");
    }
}

now my character can push the ball and cube around as you can see in the video




Unity Penelope Camera Relative Controller Summary

So here is the Unity Penelope Camera Relative Controller Summary

1. Create an empty camera pivot, under it create camera offset, under it put your camera

-camera offset, set to position 0, 0.94, 0
-camera, set to position 0, 5, -12
-drag the rotationconstraint.js and followtransform.js into camera pivot


2. Put in your main character

-drag the character controller and camera relative control.js into your main character

3. Create an empty object name it Joystick, create two empty object under it, one as leftpad, one as rightpad

-drag the guitexture and joystick.js into your leftpad and rightpad

-set the empty joystick to location 0,0,0
-set the texture of guitexture to the button texture
-set the "leftpad" guitexture pixel in set to 30,30,100,100 (x,y, width,height)
-set the "rightpad" guitexture pixel in set to 30,355,100,100 (x,y, width,height)


Now that you have drag all the neccessary script into the object, make sure you drag all the object into the script variable

1. On the "Camera Pivot", make sure you drag your main character into followtransform.js "Target Transform", also your rotationconstraint.js put min = -15, max = 15

2. On the Main character, make sure you drag leftpad, rightpad, camera pivot, camera into the camerarelativecontrol.js variable


Saturday, December 1, 2012

Today Progress: Finally I am able to test my unity game on my iPhone

So after paying USD400 for the unity IOS license and USD100 for the iPhone developer license, fix here and there, finally I am able to test my unity games on my iPhone


Yeah, just 2 joystick one to move the character around, and one to rotate around the screen


Unity How to Shrink your app size

Well, just like others I want to test out an empty unity project to see how big it is once it is installed into my iPhone.

So here it goes

first attempt, build into IOS, open IOS project in Xcode, Build and run -> 60 MB

60 MB !!!!!!!!! HOLY SHIT, I GOT EMPTY UNITY PROJECT

after googling for an hour and test out this and that

below is the step

Step 1: On your Unity,  File -> Build Setting -> Player Setting, on the IOS tab(3rd one), Target Device -> set to iPHONE ONLY
Target Platform set to armv7

I repeat the step to push it into my iPhone and now it show -> 35 MB

HOLY MOLY !!!!!!


Step 2: On your Xcode, Product -> Edit Schema, on the left hand side "run" tab, then middle "Info" tab , change the build configuration to "release"

Now I repeat the step to push it into my iPhone now it show -> 22.7 MB


I read many tutorial all asking you to buy the USD1500 unity pro which will further shrink down your app to 15MB, sigh, I am very poor, but if I am rich a bit I might pay USD1500 to shrink that 7 MB


Error: unknown error code. This generally means that another instance of this process was already running or is hung in the debugger

I got this error:

Error: unknown error code. This generally means that another instance of this process was already running or is hung in the debugger

Seem like even the empty app without anything I will get this error,

So I tried to fix it
1. Restart Xcode
2. Restart Computer
3. Restart your iPhone


Then i get this error


Assertion: should not be reached at tramp-arm.c:724


Finally, it turn out to be causing by Unity iPhone Simulator -> Your Device

meaning, you just need to go to the Xcode, on top left, then change it to Unity iPhone -> Your Device

then you can see it run perfectly on your iPhone

I wonder why this is not mention anywhere on the tutorial







Simulate Unity Penelope using mouse control

Below is the forum thread i found, tested and working

http://forum.unity3d.com/threads/54800-Is-there-a-way-to-simulate-the-Touch-using-the-mouse



Unity Penelope things I don't understand

Well, I have downloaded the unity penelope sample into my unity

however there are many problem with this app that I don't understand

1. When i try to build the unity version into IOS, it just fail to run in IOS with zero error, it just quit within 1 second
2. When I download the penelope app from the appstore, it is working fine, when i check the size it eat, it only eat 16 MB, but when i build empty project it eat 64MB, where did the 50MB coming from
3. Dragging the camera controller script into my own character will only work in unity but not when i build it into the IOS

sigh, rough road again, I am stuck!!!!!! if there are any expert out there who can help, PLEASE HELP ME!!!!