Unity 3D Script Note

1.Shoot,Throw|Instantiate

var speed = 3.0;
var grenadePrefab:Transform; 

function Update () 
{
    //find out if a fire button is pressed
    if(Input.GetButtonDown("Fire1"))
    {
        if(Collisions.GRENADE_AMMO > 0)
        {
            //create the prefab
            var grenade = Instantiate(grenadePrefab, transform.position, Quaternion.identity);
        
            //add force to the prefab
            grenade.rigidbody.AddForce(transform.forward * 2000);
            
            Collisions.GRENADE_AMMO --;
       //modify the gui text.
            GameObject.Find("g_Count").guiText.text = ""+Collisions.GRENADE_AMMO;//"" is to convent the int to string
            print("YOU NOW HAVE "+ Collisions.GRENADE_AMMO +" GRENADES");
        }
    }
}

 

2.Collision(Raycast) ,PlayAnimation

static var GRENADE_AMMO = 0;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
    if(hit.gameObject.tag == "crateGrenades")
    {
        //destroy the ammo box
        Destroy(hit.gameObject);
        
        //add ammo to inventory
        GRENADE_AMMO += 8;
        print("YOU NOW HAVE "+ GRENADE_AMMO +" GRENADES");
        GameObject.Find("g_Count").guiText.text = ""+GRENADE_AMMO;
    }
}


var rayCastLength = 5;

function Update()
{    
    var hit : RaycastHit;
    
    //check if we're colliding
    if(Physics.Raycast(transform.position, transform.forward, hit, rayCastLength))
    {
        //...with a door
        if(hit.collider.gameObject.tag == "door")
        {
            //open the door
            hit.collider.gameObject.animation.Play("door_open");
        }
    }
}

 

3.Grenade Explosion

var creationTime = Time.time;
var explosionPrefab : Transform;
var lifeTime = 3;

function Awake()
{
    creationTime = Time.time;
}

function Update () 
{
    if(Time.time > (creationTime+lifeTime))
    {
        Destroy(gameObject);
        Instantiate(explosionPrefab, transform.position, Quaternion.identity);
    }
}

 4.MenuItem:

var isQuitBtn = false;

function OnMouseEnter () 
{
    renderer.material.color = Color.red;
}

function OnMouseExit () 
{
    renderer.material.color = Color.white;
}

function OnMouseUp()
{
    if(isQuitBtn)
    {
        Application.Quit();
    }
    else
    {
        Application.LoadLevel(1);
    }
}

 5.Denfender AI (Look at slowly,Visible range,InvokeRepeating)

var target : Transform;
var range = 10.0;
var leftFlame : GameObject;
var rightFlame : GameObject;
static var mode = "idle";
var gunSpeed = 0.05;

function Awake()
{
    target = GameObject.FindWithTag("Player").transform;
    leftFlame.renderer.enabled = false;
    rightFlame.renderer.enabled = false;
}

function Update () 
{    
    if(target && CanAttackTarget())
    {
        //transform.LookAt(target);
        var targetRotation = Quaternion.LookRotation(target.position - transform.position, Vector3.up);
        transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 1.2);
        
        var forward = transform.forward;
        var targetDir = target.position - transform.position;
        var angle = Vector3.Angle(targetDir, forward);
        if(angle < 10.0)
        {
            DoDamage();
        }
        else
        {
        }
    }
}

var damageTimer = 0.0;

function DoDamage()
{
    if(damageTimer==0.0)
    {
        damageTimer = Time.time;
    }
    if((damageTimer+0.05) > Time.time)
    {
        return;
    }
    else
    {
        Player.HEALTH -= 1;
        print(Player.HEALTH);
        damageTimer = Time.time;
    }
}

function CanAttackTarget()
{
    //Check if the target is close enough
    if(Vector3.Distance(transform.position, target.position) > range)
    {
        Disengage();
        return false;
    }
    
    var hit : RaycastHit;
    
    //Check if there's collision inbetween turret & target
    if(Physics.Linecast(transform.position, target.position, hit))
    {
        if(hit.collider.gameObject.tag != "Player")
        {
            Disengage();
            return false;
        }
        else
        {
            Attack();
            return true;
        }
    }
    return true;
}

function Attack()
{
    if(mode != "attack")
    {
        InvokeRepeating("FalconAnimate", 2, gunSpeed);
        mode = "attack";
    }
}

function Disengage()
{
    if(mode != "idle")
    {
        CancelInvoke();
        mode = "idle";
        leftFlame.renderer.enabled = false;
        rightFlame.renderer.enabled = false;
    }
}

function FalconAnimate()
{
    if(leftFlame && rightFlame)
    {
        if(leftFlame.renderer.enabled)
        {
            leftFlame.renderer.enabled = false;
            rightFlame.renderer.enabled = true;
        }
        else
        {
            leftFlame.renderer.enabled = true;
            rightFlame.renderer.enabled = false;
        }
    }
    else
    {
        print("Effects on Turret not set!");
    }
}

6.GUI

a.Set Text

  GameObject.Find("g_Count").guiText.text = ""+5;

b.Set Texture

  var h80 : Texture2D;

  GameObject.Find("g_Health").guiTexture.texture=h80;

7.Audio

var audiofile : AudioClip;

audiofile.PlayOneShot();

//audiofile.Play();

posted @ 2013-01-02 20:18  Qiengo  阅读(412)  评论(0编辑  收藏  举报