Unity3D 5.x 簡單實例 - 腳本編寫


1,Vector3 類型變量存儲向量坐標值

Vector3.forward

Vector3(0,0,1)

Vector3.up 

Vector3(0,1,0)

Vector3.right

Vector3(1,0,0)

Vector3.zero

Vector3(0,0,0)

Vector3.one

Vector3(1,1,1)

2,給對象RigidBody添加組件 ,然后給RigidBody一個速率(velocity)讓它的移動

using UnityEngine;
using System.Collections;

public class moveFwd : MonoBehaviour
{

    public float moveSpeed = 0.1f; 

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    { 
        Vector3 moveForward = new Vector3(moveSpeed, 0 , moveSpeed);
        this.GetComponent<Rigidbody>().velocity = moveForward;  
    }
}
View Code

  

3, 鼠標移入變紅色,鼠標移出還原顏色,點擊鼠標播放聲音,JS代碼實現:

#pragma strict

var oldColor:Color;
var audioPlay=false;

function Start () {
    oldColor= this.GetComponent(MeshRenderer).material.color;
}


function Update () {

}

//鼠標移入
function OnMouseOver(){
    this.GetComponent(MeshRenderer).material.color=Color.red;   

    //旋轉
    this.transform.Rotate(0,-25*Time.deltaTime,0);
    print("OnMouseOver");
}
//鼠標移出
function OnMouseExit(){
    this.GetComponent(MeshRenderer).material.color=oldColor;   
    print("OnMouseOut"); 
}
//點擊鼠標
function OnMouseDown(){
    if (audioPlay==false) {
        this.GetComponent(AudioSource).Play();
        audioPlay=true;
    }else {
        this.GetComponent(AudioSource).Pause();
        audioPlay=false;
    }
    print("OnMouseDown"); 

}
View Code

 4, 操作文本的時候,記得先引用  UnityEngine.UI:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class count : MonoBehaviour
{  
    void OnMouseDown() {
        GameObject.Find("Canvas/Text").GetComponent<Text>().text = "Score";
    }
}
View Code

 5,給小球添加物理材質,是小球跳動:Asseets → Create → Physic Material :

  

  JS控制小球顏色變化:

var t:float=0; 
function Update () {  
  t+=Time.deltaTime;  
  //每3秒換一個隨機顏色
  if(parseInt(t)%3==0){ 
    gameObject.Find("Sphere").GetComponent(MeshRenderer).material.color=Color(Random.Range(0,255)/255f,Random.Range(0,255)/255f,Random.Range(0,255)/255f);
  }  
   
}
View Code

6, 獲取對象放在Start()方法里面,不要放到Update()里面,這樣會提高運行效率,JS控制燈光變強、變弱:

#pragma strict
import UnityEngine.Light; 

var directLight:GameObject;
var theTxt:GameObject;
var objCube:GameObject;

function Start () { 
    //獲取對象
    directLight=gameObject.Find("Directional Light");
    theTxt=gameObject.Find("Canvas/Text");
    objCube=gameObject.Find("Cube");
} 

function Update () {  
 
  //更改燈光亮度
  if (Input.GetKey(KeyCode.L)) {
    directLight.GetComponent(Light).intensity+=0.1;
  };
  if (Input.GetKey(KeyCode.K)) {
     directLight.GetComponent(Light).intensity-=0.1;  
  };
  theTxt.GetComponent(Text).text = directLight.GetComponent(Light).intensity.ToString(); 
   if (Input.GetKey(KeyCode.S)) {
       //調用了Cube組件中的go方法
      objCube.SendMessage("go");
  };
}
View Code

7,代碼實現第一人稱控制器,思路:

  ①新建項目,添加地面Plane,創建3D Object(Capsule) ,添加Cute

  ②給Capsule附加腳本讓其能夠上下左右移動

  ③把Main Camera的Position調節和Capsule一致、略高,然后把MainCamera作為Capsule的Child Object

  JS 代碼:

#pragma strict
@script RequireComponent(CharacterController)

var speed:float=6.0;
var jumpspeed:float =8.0;
var gravity:float =1.0;
private var movedirection:Vector3=Vector3.zero;
private var grounded:boolean=false;

function FixedUpdate(){ 
        if (grounded){
            movedirection=Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
            
            movedirection=transform.TransformDirection(movedirection);
            movedirection*=speed ;
            
            if(Input.GetButton("Jump")){
                movedirection.y=jumpspeed;            
            }     
        }
        movedirection.y -= gravity*Time.deltaTime;    
        
        var controller:CharacterController=GetComponent(CharacterController);
        //移動命令
        
        var flags=controller.Move(movedirection*Time.deltaTime);
        //CollisionFlags.CollidedBelow    底部發生了碰撞“flags & CollisionFlags.CollidedBelow”返回1
        //CollisionFlags.CollidedNone   沒發生碰撞“flags & CollisionFlags.CollidedNone”返回1
        //CollisionFlags.CollidedSides    四周發生了碰撞“flags & CollisionFlags.CollidedSides”返回1
        //CollisionFlags.CollidedAbove   頂端發生了碰撞“flags & CollisionFlags.CollidedAbove”返回1
        // 單個& 表示比較兩個二進制數值
        //位掩碼技術
        grounded=(flags & CollisionFlags.CollidedBelow)!=0;  
}
View Code

 

 

參考 : 

1,Unity3D中MeshRenderer的使用

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM