Skip to main content

摄像机碰到后面物体缩进视口(消隐,滚轴,距离)

var camera1:Camera;           //摄像机

var mousescrollwheel:float; //鼠标中间滚轮,不要修改默认值
var sensitivityW:float=5; //鼠标滚轮敏捷性

var nearview:float=0; //鼠标滚轮滚动 控制摄像机离设定点的最近距离
var farview:float=-10; //鼠标滚轮滚动 控制摄像机离设定点的最远距离
function Start ()
{
mousescrollwheel=camera1.transform.localPosition.z;
}

function Update () {
if (camera1==null){
return;
}
mousescrollwheel += Input.GetAxis("Mouse ScrollWheel")*sensitivityW;
if(mousescrollwheel>nearview)
{
mousescrollwheel=nearview;
}
else if(mousescrollwheel<farview)
{
mousescrollwheel=farview;
}
camera1.transform.localPosition.z=mousescrollwheel;
var fwd = -transform.TransformDirection (Vector3.forward);

var hit : RaycastHit;
if (Physics.Raycast (transform.position, fwd, hit, -farview))
{
if(hit.transform.tag!="renwu") //当检查到的物体的标签不为人物的时候。
{
var cameraz =(hit.point- transform.position).magnitude;
print(cameraz);
if(-cameraz>mousescrollwheel) //摄像机与背后物体的距离大于滚轴的距离时
{
camera1.transform.localPosition.z=-cameraz;
}
}
}

//人物透明
if(camera1.transform.localPosition.z>=-1.5&&camera1.transform.localPosition.z<=-0.8)
{
renderer.material.color.a = 0.3;
}
else if(camera1.transform.localPosition.z>-0.8)
{
renderer.material.color.a = 0;
}
else
{
renderer.material.color.a =1;
}

}