最近在做一个模块,要求使用暴风魔镜SDK通过头部和手柄控制字体拼凑,注意不是笔划的书写哦,以为用头来控制写字实在是太奇怪了......

以“鸟”字为例,其他的字也可以,只需设置好模板上笔划应到的位置,以及按顺序给笔划编号如1,2,3....即可。效果如下,肯定不完美,但还是要记录下来,希望能与大家多交流啊,哈哈哈哈

unity手柄插件_ide

通过按下魔镜C键来选择下方的笔划,通过头部移动来控制笔划的移动

C键选择时必须按照标准的笔划顺序才能选取

unity手柄插件_System_02

unity手柄插件_unity手柄插件_03

实现思路基本是射线

1.通过射线检测所有物体,当找到tag为Font的物体后,即确定了想要的笔划。

2.通过射线检测所有物体,控制笔划始终与头部发射射线触碰点相一致。

3.以该笔划为发射点向墙体发射射线,碰到需要到达的地方之后,就自动对齐。

需要放置笔划最终想要到达的点,如下

unity手柄插件_System_04

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

unity手柄插件_unity手柄插件_05

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

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

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

代码如下:

选择笔划及控制移动

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
  
    public class FontMove : MonoBehaviour  
    {  
  
        public static FontMove _instance;  
  
        private GameObject lacuchPosition;//射线发射位置  
        public GameObject targetMoveObject = null;//被拾起的笔画  
  
        public LayerMask moveLayer;//移动层  
        public LayerMask wallLayer;//墙体层  
  
        public bool isMove = false;//是否可以移动  
        public bool isFind = true;//是否可以寻找  
  
        private Vector3 lastPosition;  
        private int strokeNumber=1;//记录笔画顺序  
  
        void Awake()  
        {  
            _instance = this;  
        }  
  
        void Start()  
        {  
            lacuchPosition = GameObject.FindGameObjectWithTag("MainCamera");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            FindTarget();  
            if (isMove == true)  
                TargetMove();  
        }  
  
        void FindTarget()  
        {//寻找需要选择的笔画  
            if (isFind == true)  
                if (CrossPlatformInputManager.GetButtonDown("C") || Input.GetKeyDown(KeyCode.J))  
                {  
  
                    RaycastHit info;  
                    bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 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++;  
                            }  
                        }  
                    }  
                }  
        }  
  
        void TargetMove()  
        {//移动笔画  
            if (targetMoveObject != null)  
            {  
                RaycastHit info;  
                bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 100);  
                if (hit)  
                {  
                    if (info.transform.tag == "Wall")  
                    {  
                        Vector3 pos = info.point;  
                        pos -= new Vector3(0.1f, 0, 0);  
                        targetMoveObject.transform.position = pos;  
                    }  
                }  
            }  
        }  
    }  
}

控制笔划匹配

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
    public class FontAlign : MonoBehaviour  
    {  
        private GameObject[] wallStrokePosition;//墙上应该被放置的笔画的位置  
        private GameObject lacuchPosition;//笔画向墙发射射线的位置  
  
        private GameObject targetStroke = null;//墙上被选中的笔画  
  
        // Use this for initialization  
        void Start()  
        {  
            wallStrokePosition = GameObject.FindGameObjectsWithTag("wallStrokePosition");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            if (FontMove._instance.targetMoveObject != null)  
            {  
                lacuchPosition = FontMove._instance.targetMoveObject;  
            }  
            FindPosition();  
        }  
        void FindPosition()  
        {  
            if (lacuchPosition != null)  
            {  
                foreach (GameObject go in wallStrokePosition)  
                {  
                    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;  
                }  
            }  
  
        }  
    }  
  
}

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

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
  
    public class FontMove : MonoBehaviour  
    {  
  
        public static FontMove _instance;  
  
        private GameObject lacuchPosition;//射线发射位置  
        public GameObject targetMoveObject = null;//被拾起的笔画  
  
        public LayerMask moveLayer;//移动层  
        public LayerMask wallLayer;//墙体层  
  
        public bool isMove = false;//是否可以移动  
        public bool isFind = true;//是否可以寻找  
  
        private Vector3 lastPosition;  
        private int strokeNumber=1;//记录笔画顺序  
  
        void Awake()  
        {  
            _instance = this;  
        }  
  
        void Start()  
        {  
            lacuchPosition = GameObject.FindGameObjectWithTag("MainCamera");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            FindTarget();  
            if (isMove == true)  
                TargetMove();  
        }  
  
        void FindTarget()  
        {//寻找需要选择的笔画  
            if (isFind == true)  
                if (CrossPlatformInputManager.GetButtonDown("C") || Input.GetKeyDown(KeyCode.J))  
                {  
  
                    RaycastHit info;  
                    bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 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++;  
                            }  
                        }  
                    }  
                }  
        }  
  
        void TargetMove()  
        {//移动笔画  
            if (targetMoveObject != null)  
            {  
                RaycastHit info;  
                bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 100);  
                if (hit)  
                {  
                    if (info.transform.tag == "Wall")  
                    {  
                        Vector3 pos = info.point;  
                        pos -= new Vector3(0.1f, 0, 0);  
                        targetMoveObject.transform.position = pos;  
                    }  
                }  
            }  
        }  
    }  
}

控制笔划匹配

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
    public class FontAlign : MonoBehaviour  
    {  
        private GameObject[] wallStrokePosition;//墙上应该被放置的笔画的位置  
        private GameObject lacuchPosition;//笔画向墙发射射线的位置  
  
        private GameObject targetStroke = null;//墙上被选中的笔画  
  
        // Use this for initialization  
        void Start()  
        {  
            wallStrokePosition = GameObject.FindGameObjectsWithTag("wallStrokePosition");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            if (FontMove._instance.targetMoveObject != null)  
            {  
                lacuchPosition = FontMove._instance.targetMoveObject;  
            }  
            FindPosition();  
        }  
        void FindPosition()  
        {  
            if (lacuchPosition != null)  
            {  
                foreach (GameObject go in wallStrokePosition)  
                {  
                    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;  
                }  
            }  
  
        }  
    }  
  
}

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

控制笔划匹配

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
    public class FontAlign : MonoBehaviour  
    {  
        private GameObject[] wallStrokePosition;//墙上应该被放置的笔画的位置  
        private GameObject lacuchPosition;//笔画向墙发射射线的位置  
  
        private GameObject targetStroke = null;//墙上被选中的笔画  
  
        // Use this for initialization  
        void Start()  
        {  
            wallStrokePosition = GameObject.FindGameObjectsWithTag("wallStrokePosition");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            if (FontMove._instance.targetMoveObject != null)  
            {  
                lacuchPosition = FontMove._instance.targetMoveObject;  
            }  
            FindPosition();  
        }  
        void FindPosition()  
        {  
            if (lacuchPosition != null)  
            {  
                foreach (GameObject go in wallStrokePosition)  
                {  
                    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;  
                }  
            }  
  
        }  
    }  
  
}

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

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
  
    public class FontMove : MonoBehaviour  
    {  
  
        public static FontMove _instance;  
  
        private GameObject lacuchPosition;//射线发射位置  
        public GameObject targetMoveObject = null;//被拾起的笔画  
  
        public LayerMask moveLayer;//移动层  
        public LayerMask wallLayer;//墙体层  
  
        public bool isMove = false;//是否可以移动  
        public bool isFind = true;//是否可以寻找  
  
        private Vector3 lastPosition;  
        private int strokeNumber=1;//记录笔画顺序  
  
        void Awake()  
        {  
            _instance = this;  
        }  
  
        void Start()  
        {  
            lacuchPosition = GameObject.FindGameObjectWithTag("MainCamera");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            FindTarget();  
            if (isMove == true)  
                TargetMove();  
        }  
  
        void FindTarget()  
        {//寻找需要选择的笔画  
            if (isFind == true)  
                if (CrossPlatformInputManager.GetButtonDown("C") || Input.GetKeyDown(KeyCode.J))  
                {  
  
                    RaycastHit info;  
                    bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 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++;  
                            }  
                        }  
                    }  
                }  
        }  
  
        void TargetMove()  
        {//移动笔画  
            if (targetMoveObject != null)  
            {  
                RaycastHit info;  
                bool hit = Physics.Raycast(lacuchPosition.transform.position, lacuchPosition.transform.forward, out info, 100);  
                if (hit)  
                {  
                    if (info.transform.tag == "Wall")  
                    {  
                        Vector3 pos = info.point;  
                        pos -= new Vector3(0.1f, 0, 0);  
                        targetMoveObject.transform.position = pos;  
                    }  
                }  
            }  
        }  
    }  
}

控制笔划匹配

using UnityEngine;  
using System.Collections;  
namespace MojingSample.CrossPlatformInput.PlatformSpecific  
{  
    public class FontAlign : MonoBehaviour  
    {  
        private GameObject[] wallStrokePosition;//墙上应该被放置的笔画的位置  
        private GameObject lacuchPosition;//笔画向墙发射射线的位置  
  
        private GameObject targetStroke = null;//墙上被选中的笔画  
  
        // Use this for initialization  
        void Start()  
        {  
            wallStrokePosition = GameObject.FindGameObjectsWithTag("wallStrokePosition");  
        }  
  
        // Update is called once per frame  
        void Update()  
        {  
            if (FontMove._instance.targetMoveObject != null)  
            {  
                lacuchPosition = FontMove._instance.targetMoveObject;  
            }  
            FindPosition();  
        }  
        void FindPosition()  
        {  
            if (lacuchPosition != null)  
            {  
                foreach (GameObject go in wallStrokePosition)  
                {  
                    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;  
                }  
            }  
  
        }  
    }  
  
}

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