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

 

 

转载于:https://www.cnblogs.com/i-shanghai/p/5570144.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本书对Unity 3D集成开发环境界面、脚本编写和众多高级特效的实现进行了详细介绍,内容深入浅出,是一本适合不同需求、不同开发水平读者的技术宝典。 全书共分16章。第1章主要介绍了Unity 3D的诞生、特点、开发环境的搭建及运行机制;第2章对Unity 3D集成开发环境进行了详细介绍;第3章介绍了Unity 3D中脚本编写;第4章主要对Unity 3D开发过程中经常使用的组件及对象进行了详细介绍;第5章介绍了Unity游戏开发中非常流行的第三方UI界面开发组件库—NGUI的基础知识;第6章介绍了Unity开发平台的完整的物理引擎体系;第7章介绍了Unity 3D中的着色器和着色器语言—ShaderLab;第8章介绍了天空盒、虚拟按钮与摇杆、声音、水特效、3D拾取、重力加速度传感器及雾特效等开发常用的技术;第9章介绍了Unity中经常使用的光影效果,主要包括各种光源、光照烘焙、法线贴图、镜面特效、波动水面真实效果等技术;第10章介绍了Unity中模型的网格概念及新旧动画系统;第11章介绍了Unity自带的地形引擎、拖尾渲染及导航网格和寻路系统等知识;第12章介绍了AssetBundle更新资源包的使用;第13章介绍了Unity中的多线程技术与网络开发;第14章介绍了Unity 2D游戏开发工具;第15章介绍了Unity 3D提供的Profiler工具的使用方法,及断点调试的两种方式;第16章介绍了完整的大型3D游戏案例—指间足球。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值