工程如下图,按照笔划顺序给笔划命名
![](http://image106.360doc.com/DownloadImg/2017/05/1210/98895435_1)
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;
- }
- }
-
- }
- }
-
- }
有不足的地方或者更好地解决办法希望大家能指出哦
|