Unity 3D Game Engine Tips (part 2)

I have recently started working on a mobile game using the Unity 3D engine. I will be adding code snippets here that I think might help out someone out there. Check out my other post related Unity 3D game engine tips.

Move Camera (game object) using the accelerometer by tilting the phone

This example was using for my first person control but the same idea can be used for any game object.

var speed : float = .01; // how much to move the camera/game object
var moveThreshold : float = 10; // how the phone has to tilt before any movement 
private var iPx : float; 

function Update()
{

    iPx = Input.acceleration.x;

    if (Mathf.Abs(iPx) > moveThreshold) {
        movex = Mathf.Sign(iPx) * speed;
        this.transform.Rotate(0,  iPx, 0, Space.World);  // I have mine rotating on the y-axis 
        this.transform.position += Vector3(0, 0, -iPx); 
    }
}

Smooth Rotating of an Game Object using Input.acceleration

The code below use the phone’s accelerometer to rotate the camera but could be used for any controller (i.e. third person).

static var iPx : Vector3; 
var moveThreshold : float = 10

function FixedUpdate()
{
    iPx = Input.acceleration;
	    if (Mathf.Abs(iPx.x) > moveThreshold) {
		Camera.main.transform.RotateAroundLocal(Vector3.up, iPx.x * .5 * Time.deltaTime);
	    }
}

Find the vector the camera is pointing / looking at

This will return a Vector3.

// camera
Camera.main.transform.forward.normalized;
//some game object
this.transform.forward.normalized;

Rotate Camera While Going up Terrain

I been working on a game where I need to know when the player is going up a terrain so I can rotate the camera upwards. I used a ray cast to know when the terrain is approaching then I rotated the camera 1 step at a time till it is no longer hitting the terrain. The second function is used to rotate the camera back when it is no longer climbing the terrain. T

var positionPrevious = Vector3(0, 0 ,0);
var position = Vector3(0, 0, 0);
function Update(){
position = GameObject.Find("Capsule").transform.position; 
        /* going up terrain  */
        var fwd = transform.TransformDirection (Vector3.forward);
	var hit : RaycastHit;
	if (Physics.Raycast (transform.position, Camera.main.transform.forward.normalized * 20, 10)) {
	       flatSurface = false;
	        Debug.DrawLine (transform.position, hit.point);
	        Camera.main.transform.Rotate( -1 , 0, 0);
	        print ("There is something in front of the object! " + transform.position);       
	  }

        /* tilting back from going up terrain */
        if( (flatSurface ==false) && (position.y == positionPrevious.y) ){
            var rot = Camera.main.transform.rotation.x; 
        	Camera.main.transform.Rotate(-rot/10 , 0, 0); 
        	if(rot < 3 && rot > 358 ) flatSurface = true;
        }  
 positionPrevious = position;
 }

Save a Top Score and Getting High Score

I am using the PlayerPrefs class to store the values. If the player score is higher then the already set score then the stored value in PlayerPref will be updated.

/* save top score */
function saveWin(){
   if(score > PlayerPrefs.GetInt("PlayersHighestSCORE", 0)){ 
 	  PlayerPrefs.SetInt("PlayersHighestSCORE" , score);
   }
}

/* Get Top Score  */

function OnGUI(){
	    GUI.Box(Rect (100, 100, 350, 20), "Your Highest Score =  " + 
			PlayerPrefs.GetInt("PlayersHighestSCORE", 999) );  //I added 999 in case of error
}


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