- 最后登录
- 2017-6-23
- 注册时间
- 2012-2-18
- 阅读权限
- 90
- 积分
- 33060
  
- 纳金币
- 32449
- 精华
- 23
|
最近做一个小室内,用按钮控制相机的走位,但是有个问题出现了
就是相机能出房间的墙面,就是说没有了碰撞,所以问问大家有什么好的解决方法。
我也把代码发一下,以供参考。
public var target:Transform;
public var moveSpeed=1;
public var rotateAngle=20;
function Start(){
if(!target){
print("not set target!");
var go=GameObject.CreatePrimitive( PrimitiveType.Cube);
target=go.transform;
target.position=Camera.main.transform.TransformPoint(Vector3(0,0,5));
target.rotation=Camera.main.transform.rotation;
}
}
function OnGUI(){
var width=60;
var height=40;
GUI.BeginGroup(Rect((Screen.width-width*3)/2,Screen.height-height*2,width*3,height*2));
var moveDirection=Vector3.zero;
if(GUI.Button(Rect(width,0,width,height),"forward")){
moveDirection.z=1;
}
if(GUI.Button(Rect(width,height,width,height),"back")){
moveDirection.z=-1;
}
if(GUI.Button(Rect(0,height,width,height),"left")){
moveDirection.x=-1;
}
if(GUI.Button(Rect(width*2,height,width,height),"right")){
moveDirection.x=1;
}
if(GUI.Button(Rect(0,0,width,height),"L-rotate")){
//moveDirection.x=-1;
transform.Rotate(0,-Time.deltaTime*rotateAngle*10, 0);
}
if(GUI.Button(Rect(width*2,0,width,height),"R-rotate")){
//moveDirection.x=1;
transform.Rotate(0, Time.deltaTime*rotateAngle*10, 0);
}
if(target){
moveDirection=moveDirection*moveSpeed;
target.position=target.position+ target.rotation*moveDirection;
}
GUI.EndGroup();
}
![]() ![]()
|
|