查看: 1190|回复: 1
打印 上一主题 下一主题

UNTIY3D 用按钮控制如何控制不出房间的墙体呢?

[复制链接]

2206

主题

2

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
32449
精华
23

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2013-2-22 08:38:48 |只看该作者 |倒序浏览
最近做一个小室内,用按钮控制相机的走位,但是有个问题出现了

就是相机能出房间的墙面,就是说没有了碰撞,所以问问大家有什么好的解决方法。

我也把代码发一下,以供参考。


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();
     
   
    }

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

2722

主题

42

听众

3万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
38268
精华
111

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2013-2-28 01:35:17 |只看该作者
希望在新的一年里专心的学习好Unity!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-7-29 02:42 , Processed in 0.060684 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部