分享

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

 文清阳 2017-05-12

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


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

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

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

代码如下:

选择笔划及控制移动

  1. using UnityEngine;  
  2. using System.Collections;  
  3. namespace MojingSample.CrossPlatformInput.PlatformSpecific  
  4. {  
  5.   
  6.     public class FontMove : MonoBehaviour  
  7.     {  
  8.   
  9.         public static FontMove _instance;  
  10.   
  11.         private GameObject lacuchPosition;//射线发射位置  
  12.         public GameObject targetMoveObject = null;//被拾起的笔画  
  13.   
  14.         public LayerMask moveLayer;//移动层  
  15.         public LayerMask wallLayer;//墙体层  
  16.   
  17.         public bool isMove = false;//是否可以移动  
  18.         public bool isFind = true;//是否可以寻找  
  19.   
  20.         private Vector3 lastPosition;  
  21.         private int strokeNumber=1;//记录笔画顺序  
  22.   
  23.         void Awake()  
  24.         {  
  25.             _instance = this;  
  26.         }  
  27.   
  28.         void Start()  
  29.         {  
  30.             lacuchPosition = GameObject.FindGameObjectWithTag("MainCamera");  
  31.         }  
  32.   
  33.         // Update is called once per frame  
  34.         void Update()  
  35.         {  
  36.             FindTarget();  
  37.             if (isMove == true)  
  38.                 TargetMove();  
  39.         }  
  40.   
  41.         void FindTarget()  
  42.         {//寻找需要选择的笔画  
  43.             if (isFind == true)  
  44.                 if (CrossPlatformInputManager.GetButtonDown("C") || Input.GetKeyDown(KeyCode.J))  
  45.                 {  
  46.   
  47.                     RaycastHit info;  
  48.                     bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 100);  
  49.                     if (hit)  
  50.                     {  
  51.                         if (info.transform.tag == "Font")  
  52.                         {  
  53.                             if (info.transform.name == strokeNumber.ToString())  
  54.                             {//如果满足要求的笔划顺序  
  55.                                 targetMoveObject = info.transform.gameObject;  
  56.                                 lastPosition = targetMoveObject.transform.position;  
  57.                                 isMove = true;  
  58.                                 isFind = false;  
  59.                                 targetMoveObject.GetComponent<BoxCollider>().enabled = false;  
  60.                                 strokeNumber++;  
  61.                             }  
  62.                         }  
  63.                     }  
  64.                 }  
  65.         }  
  66.   
  67.         void TargetMove()  
  68.         {//移动笔画  
  69.             if (targetMoveObject != null)  
  70.             {  
  71.                 RaycastHit info;  
  72.                 bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 100);  
  73.                 if (hit)  
  74.                 {  
  75.                     if (info.transform.tag == "Wall")  
  76.                     {  
  77.                         Vector3 pos = info.point;  
  78.                         pos -= new Vector3(0.1f, 0, 0);  
  79.                         targetMoveObject.transform.position = pos;  
  80.                     }  
  81.                 }  
  82.             }  
  83.         }  
  84.     }  
  85. }  
控制笔划匹配

  1. using UnityEngine;  
  2. using System.Collections;  
  3. namespace MojingSample.CrossPlatformInput.PlatformSpecific  
  4. {  
  5.     public class FontAlign : MonoBehaviour  
  6.     {  
  7.         private GameObject[] wallStrokePosition;//墙上应该被放置的笔画的位置  
  8.         private GameObject lacuchPosition;//笔画向墙发射射线的位置  
  9.   
  10.         private GameObject targetStroke = null;//墙上被选中的笔画  
  11.   
  12.         // Use this for initialization  
  13.         void Start()  
  14.         {  
  15.             wallStrokePosition = GameObject.FindGameObjectsWithTag("wallStrokePosition");  
  16.         }  
  17.   
  18.         // Update is called once per frame  
  19.         void Update()  
  20.         {  
  21.             if (FontMove._instance.targetMoveObject != null)  
  22.             {  
  23.                 lacuchPosition = FontMove._instance.targetMoveObject;  
  24.             }  
  25.             FindPosition();  
  26.         }  
  27.         void FindPosition()  
  28.         {  
  29.             if (lacuchPosition != null)  
  30.             {  
  31.                 foreach (GameObject go in wallStrokePosition)  
  32.                 {  
  33.                     if (go.name == lacuchPosition.name)  
  34.                     {  
  35.                         targetStroke = go;  
  36.                     }  
  37.                 }  
  38.                 if (FontMove._instance.targetMoveObject != null)  
  39.                     if (Vector3.Distance(lacuchPosition.transform.position, targetStroke.transform.position) <= 0.5f)  
  40.                 {  
  41.                     FontMove._instance.isMove = false;  
  42.                     FontMove._instance.isFind = true;  
  43.                     FontMove._instance.targetMoveObject.transform.position = targetStroke.transform.position;  
  44.                     FontMove._instance.targetMoveObject = null;  
  45.                 }  
  46.             }  
  47.   
  48.         }  
  49.     }  
  50.   
  51. }  

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约