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的使用

 

 

posted @ 2016-06-11 13:28  i-shanghai  阅读(4578)  评论(0编辑  收藏  举报