600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Unity下使用暴风魔镜SDK通过头部和手柄控制字体拼凑(二)

Unity下使用暴风魔镜SDK通过头部和手柄控制字体拼凑(二)

时间:2024-02-28 20:02:46

相关推荐

Unity下使用暴风魔镜SDK通过头部和手柄控制字体拼凑(二)

工程如下图,按照笔划顺序给笔划命名

stroke下的物体是笔划实体,tag是Font,加碰撞体

originalStrokePositionParent下的是笔划实体原本待在的地方,tagboardStrokePosition

position是墙上希望笔划应该待在的地方,tag是wallStrokePosition,加碰撞体

代码如下:

选择笔划及控制移动

[csharp]

usingUnityEngine;

usingSystem.Collections;

namespaceMojingSample.CrossPlatformInput.PlatformSpecific

{

publicclassFontMove:MonoBehaviour

{

publicstaticFontMove_instance;

privateGameObjectlacuchPosition;//射线发射位置

publicGameObjecttargetMoveObject=null;//被拾起的笔画

publicLayerMaskmoveLayer;//移动层

publicLayerMaskwallLayer;//墙体层

publicboolisMove=false;//是否可以移动

publicboolisFind=true;//是否可以寻找

privateVector3lastPosition;

privateintstrokeNumber=1;//记录笔画顺序

voidAwake()

{

_instance=this;

}

voidStart()

{

lacuchPosition=GameObject.FindGameObjectWithTag("MainCamera");

}

//Updateiscalledonceperframe

voidUpdate()

{

FindTarget();

if(isMove==true)

TargetMove();

}

voidFindTarget()

{//寻找需要选择的笔画

if(isFind==true)

if(CrossPlatformInputManager.GetButtonDown("C")||Input.GetKeyDown(KeyCode.J))

{

RaycastHitinfo;

boolhit=Physics.Raycast(lacuchPosition.transform.position,lacuchPosition.transform.forward,outinfo,100);

if(hit)

{

if(info.transform.tag=="Font")

{

if(info.transform.name==strokeNumber.ToString())

{//如果满足要求的笔划顺序

targetMoveObject=info.transform.gameObject;

lastPosition=targetMoveObject.transform.position;

isMove=true;

isFind=false;

targetMoveObject.GetComponent<BoxCollider>().enabled=false;

strokeNumber++;

}

}

}

}

}

voidTargetMove()

{//移动笔画

if(targetMoveObject!=null)

{

RaycastHitinfo;

boolhit=Physics.Raycast(lacuchPosition.transform.position,lacuchPosition.transform.forward,outinfo,100);

if(hit)

{

if(info.transform.tag=="Wall")

{

Vector3pos=info.point;

pos-=newVector3(0.1f,0,0);

targetMoveObject.transform.position=pos;

}

}

}

}

}

}

控制笔划匹配

[csharp]

usingUnityEngine;

usingSystem.Collections;

namespaceMojingSample.CrossPlatformInput.PlatformSpecific

{

publicclassFontAlign:MonoBehaviour

{

privateGameObject[]wallStrokePosition;//墙上应该被放置的笔画的位置

privateGameObjectlacuchPosition;//笔画向墙发射射线的位置

privateGameObjecttargetStroke=null;//墙上被选中的笔画

//Usethisforinitialization

voidStart()

{

wallStrokePosition=GameObject.FindGameObjectsWithTag("wallStrokePosition");

}

//Updateiscalledonceperframe

voidUpdate()

{

if(FontMove._instance.targetMoveObject!=null)

{

lacuchPosition=FontMove._instance.targetMoveObject;

}

FindPosition();

}

voidFindPosition()

{

if(lacuchPosition!=null)

{

foreach(GameObjectgoinwallStrokePosition)

{

if(go.name==lacuchPosition.name)

{

targetStroke=go;

}

}

if(FontMove._instance.targetMoveObject!=null)

if(Vector3.Distance(lacuchPosition.transform.position,targetStroke.transform.position)<=0.5f)

{

FontMove._instance.isMove=false;

FontMove._instance.isFind=true;

FontMove._instance.targetMoveObject.transform.position=targetStroke.transform.position;

FontMove._instance.targetMoveObject=null;

}

}

}

}

}

有不足的地方或者更好地解决办法希望大家能指出哦

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。