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, , moveSpeed);
this.GetComponent<Rigidbody>().velocity = moveForward;
}
}
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(,-*Time.deltaTime,);
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"); }
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";
}
}
5,给小球添加物理材质,是小球跳动:Asseets → Create → Physic Material :
JS控制小球颜色变化:
var t:float=;
function Update () {
t+=Time.deltaTime;
//每3秒换一个随机颜色
if(parseInt(t)%==){
gameObject.Find("Sphere").GetComponent(MeshRenderer).material.color=Color(Random.Range(,)/255f,Random.Range(,)/255f,Random.Range(,)/255f);
} }
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");
};
}
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"),,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)!=;
}
参考 :