Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Friday, October 22, 2010

Unity Crash Course

This week I attended a lecture series in Unity hosted by ITU. Two nice guys from Unity, Joe and Emil, gave a guide tour through a lot of Unity's features from animation, textures, GUI and editor scripting. They covered tons of stuff and it turned out to be a nice introduction to Unity.

Adding to the greatness of it all was the time given to questions from the audience. I got to ask what was wrong with the sprite sheet animation code since it was giving my this odd graphics problem over time. At first Joe found it just as strange as me but after a trip back to the Unity-factory he could give me the solution.

It turns out the offset float value on the y-axe kept in increase over time. This would be fine if it wasn't for the fact that the float as it increased would be round up thereby constantly increasing in inaccuracy.

Solution: Adding Mathf.Repeat to the offset's x value with the length of 1.
offset.y = Mathf.Repeat(offset.y,1);
This ensures the value every time it goes above 1 is returned to count from 0 instead.

And now it works! Yaaahhhhhh!

Wednesday, September 29, 2010

Damn, Smooth Sliding

Rewriting the code the sprite sheet animation from java script to C# has given me some problems I didn't anticipate. The code simply appears to work differently in C# than in java script. I have two videos that illustrate the difference.

This video shows the right animation but the code is in java script:


Here the similar code is rewritten in C# as you will see the sprite sheet just slides at an angle instead of jumping picture by picture:


Here's the original java script code:

//vars for the whole sheet
var colCount    : int =  4;
var rowCount    : int =  1;


//vars for animation
var rowNumber   : int =  0; //Zero Indexed
var colNumber   : int =  0; //Zero Indexed
var totalCells  : int =  4;
var fps  : int = 10;
var offset  : Vector2;  //Maybe this should be a private var


//Update
function Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);  }


//SetSpriteAnimation
function SetSpriteAnimation(colCount : int,rowCount : int,rowNumber : int,colNumber : int,totalCells : int,fps : int){


    // Calculate index
    var index : int = Time.time * fps;
    // Repeat when exhausting all cells
    index = index % totalCells;
    
    // Size of every cell
    var size = Vector2 (1.0 / colCount, 1.0 / rowCount);
    
    // split into horizontal and vertical index
    var uIndex = index % colCount;
    var vIndex = index / colCount;

    // build offset
    // v coordinate is the bottom of the image in opengl so we need to invert.
    offset = Vector2 ((uIndex+colNumber) * size.x, (1.0 - size.y) - (vIndex+rowNumber) * size.y);
    
    renderer.material.SetTextureOffset ("_MainTex", offset);
    renderer.material.SetTextureScale  ("_MainTex", size);
}


And the code in my C# version:

using UnityEngine;
using System.Collections;


public class Animation_sprite_sheet : MonoBehaviour {
//vars for the whole sheet
public int colCount =  4;
public int rowCount =  1;

//vars for animation
public int rowNumber = 0; //Zero Indexed
public int colNumber = 0; //Zero Indexed
public int totalCells = 4;
public int fps = 10;
Vector2 offset;  //Maybe this should be a private var


// Use this for initialization
void Start () {
}

// Update is called once per frame
void Update () {
SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps);
}
//SetSpriteAnimation
public void SetSpriteAnimation( int colCount, int rowCount, int rowNumber, int colNumber, int totalCells, int fps){
// Calculate index
     float index = Time.time * fps;
    
// Repeat when exhausting all cells
     index = index % totalCells;
    
   // Size of every cell
     Vector2 size = new Vector2 ((1.0f / colCount), (1.0f / rowCount));
    
     // split into horizontal and vertical index
   float uIndex = index % colCount;
     float vIndex = index / colCount;

     // build offset
     // v coordinate is the bottom of the image in opengl so we need to invert.
     Vector2 offset = new Vector2 ((uIndex+colNumber) * size.x, ((1.0f - size.y) - (vIndex+rowNumber) * size.y));
    
     renderer.material.SetTextureOffset ("_MainTex", offset);
     renderer.material.SetTextureScale  ("_MainTex", size);
}
}

As all the examples I can find in unity's documentation and around the internet on unity are mostly in java script I often find myself being lost when it comes to using C# in unity.

Power of Two

I'm slowly starting to figure out how I can add graphics to the game. Basically I'm getting tired of the green, red and blue squares. So now I'll go for red, yellow(!) and green blinking circles instead.

I'll be working with sprite sheets for the animation. So far I've managed to get a clumsy animation running on the iPad by using a piece of java script I borrowed from the Unity community wiki. But I want to work in C# so now I have to rewrite it.


Oh and the "power of two"-title refers to the fact that in order to get nice graphics on the iPad I have to remember to use image sizes that are a power of two e.g. 256 x 1024.

Saturday, September 18, 2010

Keeping Their Distance

I've refined the code that determines the placement of the kids. Now the code ensures that the kids aren't placed to close. Every kid placed after the first one is checked for whether or not they are in the vicinity of any of the kids that have already been placed.

Here's a bit of code:
public bool DistanceCheck()
{
disCheck = false;

for(int i=0; i<antal-1;i++)
{
if ( ((kids[i].transform.position.x-temp.transform.position.x)*(kids[i].transform.position.x-temp.transform.position.x)) + ((kids[i].transform.position.y-temp.transform.position.y)*(kids[i].transform.position.y-temp.transform.position.y)) < 200)
{
disCheck = true;
}
}
return disCheck;
}

And a video with the latest improvement:

To sum up, reds are crying, blues are about to and green kids are okay for now. When the crying kids are tapped they turn happy (no paedophile thoughts intended).

Friday, September 17, 2010

Touchy Success


Finally the touch input is working. I thought I would be able to "talk" directly to a method on a game object from another class e.g. objectname.method. I have now realised that I needed to use the method getComponent() to access the methods of the objects. This has taken forever to realise. But now the code works! It needs refining but it works.


When I touch the screen a ray is sent along the z-axis into the 3d space (world space I guess). Whatever the ray collides with is "registered" in the raycast method's collider. Assuming the object the the ray collides with is an Kid object I can then access the methods in the kid and change it's state.

Saturday, September 11, 2010

When (0,0,0) is not (0,0,0)...


I'm working on "refining" the touch function so that a kid can sense if is being touched.
All this is probably really easy but as I'm a n00b in unity even the basic stuff is difficult. The idea is that if you touch the screen on the ipad and the coordinates of your touch matches the coordinates matches a kid-object then the kid should do something.

My problem is a can't get the coordinate system of the ipad to match the one in the unity editor. I sure it something really simple I'm missing but I can't figure it out so I've taken it to the extremes - I've posted for the first time in the unity community help forum.

Wednesday, September 8, 2010

We have touch down

After a lengthy process that most fittingly can be described as black magic I now have Unity Iphone and a iphone developer licence installed on the computer. Thanks for your help Christoffer.

I did some small changes in the code and now the prototype is on the ipad. "Only" problem is code registers touches in general on the screen and the specific location of the touch as you can see in the video.


To interact by touch instead of mouse click I changed this
void OnMouseDown()
{
gameObject.tag="smile";
  SetCryTime();
}
to this
foreach (iPhoneTouch touch in iPhoneInput.touches)
{
if(touch.phase == iPhoneTouchPhase.Began)
{
gameObject.tag="smile";
SetCryTime();
}
}
and placed the code snippet inside the update function instead of outside. Next up is fixing the touch code so it only works on the object that are actually touched.

Sunday, September 5, 2010

Organised randomness

The boxes aka the kids were in the prototype placed by hand on the scene in a grid. I did this so I could easily check if my method for making the kids near a crying child sad work. It did. So today I've worked briefly on placing the boxes/kids randomly through the code thus making the game a bite more exciting.

When placing the boxes/kids I didn't want complete randomness it would stupid if all the boxes ended up on top of each other or only in one side of the screen. I needed to organise the randomness a bit. So I've combined the random placement with a grid structure.

In the code this is accomplished with the help of a couple of for-loops. The illustration above shows the concept. For each loop A, B and C in the example there are inner loops (here illustrated with 1, 2 and 3) that picks random numbers of kids to place (here either 3 or 4) and places them at random coordinates within the intervals defines by the two for-loops. Anyway, this is how I visualised the "organised randomness" in my head before I started coding. Perhaps the code is easier to read :)

Thursday, September 2, 2010

Subversion and MonoDevelop

In trying to get more organised I've now with a little help from my friends gotten a revision control system. This gives me a bigger Ctrl+z button.

So far I've used the unitys own editor to write the c# code but as MonoDevelop comes highly recommended I've installed it. I'm looking forward to its advantages :).

I'm a n00b when it comes to installing programs on the mac. I still get surprised over how easy it is. Just drag and drop. It's somehow too easy.