- 空间 收藏于2011-02-11
LookAtObj.js
var target : Transform;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = 0;
var yMaxLimit = 80;
var offsetR:int = 90;
var initDis = 20;
var minDis = 3.0;
var maxDis = 20.0;
var mouseMode:int = 0;
var wheelSpeed = 5;
var offsetY:int =1;
var offsetX:int = 97;
var offsetZ:int = 74;
static var x = 0.0;
static var y = 0.0;
static var distance;
private var position;
private var rotation;
function Start () {
transform.position.x = -.2;
transform.position.y = .6;
transform.position.z = -4.3;
transform.Rotate(0,offsetR,0);
//transform.LookAt(target);
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
/***拖拽角度**/
function Update () {
if (target) {
distance = Vector3.Distance(target.position,transform.position);
if(Input.GetMouseButton(mouseMode)){
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
}
distance-= Input.GetAxis("Mouse ScrollWheel")*wheelSpeed;//获取鼠标中建响应
distance = Mathf.Clamp(distance,minDis,maxDis);//距离取最大值和最小值
rotation = Quaternion.Euler(y, x, 0);
position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
/**处理角度*/
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Controller.js
var _carType:GameObject[];
var _bodys:GameObject[] = [];
var _length :int;
var defaultSkin:GUISkin;
var luns:GameObject;
var textures: Texture2D[];
var lunTexture:Texture2D[];
private var status = false;
private var labels:Array ;
private var state:int = 0;
function Start(){
status = true;
}
function OnGUI(){
if(status){
labels = [];
_length = textures.length;
for(var i:int = 0; i<_length ;i++){
labels[i] = {"name":textures[i].name,"body":textures[i]};
}
GUI.skin = defaultSkin;
GUI.Box(Rect(0,Screen.height -100,Screen.width,190),"");
GUI.Label(Rect(0,Screen.height - 100,180,20),"change body's textures");
while( --_length >= 0){
createButton(180 + _length * 50,Screen.height - 100 ,labels[_length]["name"],labels[_length]["body"]);
}
GUI.Label(Rect(0,Screen.height - 70,180,20),"change car's type");
if(GUI.Button(Rect(240, Screen.height - 70,50,25),"Change")){
closeAll();
if(state == 0) state = 1;
else state = 0;
if(state == 0){
_carType[0] .renderer.enabled = true;
luns.renderer.materials[1].mainTexture = lunTexture[0];
luns.renderer.materials[2] .mainTexture= lunTexture[1];
}else{
for( var j:int =1 ; j < _carType.length; j++){
_carType[j].renderer.enabled = true;
}
luns.renderer.materials[1] .mainTexture= lunTexture[2];
luns.renderer.materials[2] .mainTexture= lunTexture[3];
}
}
}else return;
}
function closeAll():void{
if(_carType != null){
for( var i:int =0 ; i < _carType.length; i++){
_carType[i].renderer.enabled = false;
}
}
}
function createButton(x:int,y:int,_name_,_body_){
if(GUI.Button(Rect(x,y,50,25),_name_)){
doExecute(_name_,_body_);
}
}
function doExecute(_command_:String,_body_){
for(var i:int =0 ; i < _bodys.length; i++){
_bodys[i].renderer.material.mainTexture = _body_;
}
}