Unity下使用暴風魔鏡SDK通過頭部和手柄控制字型拼湊
阿新 • • 發佈:2019-01-30
最近在做一個模組,要求使用暴風魔鏡SDK通過頭部和手柄控制字型拼湊,注意不是筆劃的書寫哦,以為用頭來控制寫字實在是太奇怪了......
以“鳥”字為例,其他的字也可以,只需設定好模板上筆劃應到的位置,以及按順序給筆劃編號如1,2,3....即可。效果如下,肯定不完美,但還是要記錄下來,希望能與大家多交流啊,哈哈哈哈
通過按下魔鏡C鍵來選擇下方的筆劃,通過頭部移動來控制筆劃的移動
C鍵選擇時必須按照標準的筆劃順序才能選取
實現思路基本是射線
1.通過射線檢測所有物體,當找到tag為Font的物體後,即確定了想要的筆劃。
2.通過射線檢測所有物體,控制筆劃始終與頭部發射射線觸碰點相一致。
3.以該筆劃為發射點向牆體發射射線,碰到需要到達的地方之後,就自動對齊。
需要放置筆劃最終想要到達的點,如下
工程如下圖,按照筆劃順序給筆劃命名
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;
}
}
}
}
}
有不足的地方或者更好地解決辦法希望大家能指出哦