Unity 3D Game Engine Tips

I been recently working with Unity 3D to build a  Unity 3D game for a school project and for an android game. You can get a free version to create web based games but you will need to get paid version to go beyond that. I can not imagine a better game engine than Unity. It is very easy to learn, easy to build extra level, many built in tools and prefabs, plus you can easily move your game to different platforms (playstation3, android, web etc).

There is many tutorials online to get started so I will not bother explain how to get started. I will try though to explain some examples scripts that I used.

Firing a bullet from an enemy toward the player

The basic idea behind firing a bullet from an enemy to a player is to find a vector from enemy to player then normalize the vector.  Use that vector to know the direction to send it using the AddForce() method. You will need a bullet object with a rigid body for this to work.

var bulletAPrefab : Rigidbody;  // bullet prefab
var displacement =5; // displacement hack so the bullet will not collide with enemy 
var bulletSpeed = 5;  
var bulletInterval = 100;  // how many frames between bullets

function Update () { 
    /** find a vector from enemy to player **/
    var playerDirection = GameObject.Find("nameOfPlayer").transform.position - this.transform.position;
    playerDirection.Normalize();
    /** increase bullet speed **/
    playerDirectionSpeed = Vector3.Scale(playerDirection, Vector3(bulletSpeed, bulletSpeed, bulletSpeed));    

    if (Time.frameCount%bulletInterval == 0) { // fire after so many frames
        var clone : Rigidbody;
        /** create a bullet from prefab at the location of the enemy with a displacement **/
        clone = Instantiate(bulletAPrefab, transform.position + Vector3.Scale(playerDirection, Vector3(displacement, displacement, displacement))  , transform.rotation);
        /** add a force to the bullet **/
        clone.AddForce (playerDirectionSpeed, ForceMode.Impulse);
    }

}

Predicting a Player Movement

I am sure there is multiple ways of predicting a player’s movement but it is the way I came up with. I create two vector like before from enemy to player. One vector is created 100 (change this to suit your needs) frames before the bullet is fired then another one before the bullet is fired. I then take the difference between the two vectors to add it to the second sample. This only works if the player does not change direction between the 2 samples.

The example code is like before but with the added prediction.

var bulletAPrefab : Rigidbody;
var firstReading = Vector3(0,0,0);
var secondReading = Vector3(0,0,0);
var displacement =5; 
var bulletSpeed = 5;
var predictionMulti = 5; // fine tuning
var bulletInterval = 100;

function Update () {
    var playerDirection = GameObject.Find("Guy").transform.position - this.transform.position;
    playerDirection.Normalize();
    playerDirectionSpeed = Vector3.Scale(playerDirection, Vector3(bulletSpeed, bulletSpeed, bulletSpeed));

    if (Time.frameCount%bulletInterval == 0) {
        secondReading = playerDirection;
        prediction = Vector3.Scale(secondReading - firstReading, Vector3(predictionMulti, predictionMulti, predictionMulti)) ;
        var clone : Rigidbody;

        clone = Instantiate(bulletAPrefab, transform.position + Vector3.Scale(playerDirection, Vector3(displacement, displacement, displacement))  , transform.rotation);
	    clone.AddForce (playerDirectionSpeed + prediction, ForceMode.Impulse);
        firstReading = playerDirection;
    }

}

Collision detection examples

I had this one placed in a door object. After the player open the door, it would play a cracking sound and start another level.

var DoorOpen : AudioClip;  // to add an audio sound

function OnCollisionEnter(theCollision : Collision){   
    if(theCollision.gameObject.name == "Guy"){  // depends on the name of your player object
    audio.PlayOneShot(DoorOpen); // play the audio
    yield WaitForSeconds (3);  // wait 3 seconds before next step
    Application.LoadLevel ("endGame"); // load the new game
    }
}

Create a prefab at the location of contact

This was part of my shield. I had an explosion when a bullet hit the shield.

var explosionPrefab : Transform;
var clone : Transform ; // now available for all functions

function OnCollisionEnter(theCollision : Collision){
    if(theCollision.gameObject.name == "Cone"){
    var contact : ContactPoint = theCollision.contacts[0];
    var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal);
    var pos : Vector3 = contact.point;
    clone = Instantiate(explosionPrefab, pos, rot);
    Destroy(this.gameObject);
    clone.gameObject.tag="clone";  // this was needed to prevent all  bullet object getting killed. 
    }
}

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys