经过我们的测试,我们不难发现,根据点击的位置以及当时手机的位置的不同,放置的展览柜的旋转角度也各有不同,并不能达到我们想要的垂直的效果,主要的问题出在这个地方
gameObject.transform.Rotate(180, k_PrefabRotation, 0, Space.Self);这里对放置物体的转角做了修改。
经过讨论,与其直接将其转角固定,不如直接使用一个按钮控制,可以自己将展览柜旋转,这样一方面解决了柜子不正的问题,另一方面可以使用户自己DIY柜子的摆放角度和位置,增强了用户体验。
首先在UI界面中创建一个按钮,命名为Rotate_Button 然后修改Button_click_manager中的click_control脚本。 首先创建一个布尔变量用于存储旋转的状态
public bool BoxRotate = false;接着新建一个共有方法fastRotate,即当按住按钮时将其状态改为旋转。
public void fastRotate() { BoxRotate = true; }经过修改后的脚本click_control
using GoogleARCore.Examples.HelloAR; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class click_control : MonoBehaviour { public int click_num = 0; public int rotate_num = 0; public bool isPlay = false; public bool isRotate = false; public bool BoxRotate = false; public bool die = false; public float speed = 5f; public GameObject Plane_con; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } public void click() { click_num = click_num + 1; } public void playAudio() { isPlay = true; } public void playIsOk() { isPlay = false; } public void back() { SceneManager.LoadScene(0); } public void ToCardMode() { SceneManager.LoadScene(1); } public void ToNoCardMode() { SceneManager.LoadScene(2); } public void item_rotate() { if (rotate_num % 2 == 0) { isRotate = true; } else { isRotate = false; } rotate_num++; } public void fastRotate() { BoxRotate = true; } public void slowRotate() { BoxRotate = true; } public void refresh() { die = true; Plane_con.GetComponent<HelloARController>().hasFile = false; } }并在挂在在预制体上的脚本change_items中的Update方法中,加入对于刚刚脚本中的变量**BoxRotate 的检测,如果检测其为true则旋转一个小角度并将BoxRotate **置为false
if (button_control.GetComponent<click_control>().BoxRotate) { transform.Rotate(new Vector3(0, 1, 0)); button_control.GetComponent<click_control>().BoxRotate = false; }经过修改后的脚本change_items
using System.Collections; using System.Collections.Generic; using UnityEngine; public class change_items : MonoBehaviour { public GameObject allitem; public GameObject[] items; public GameObject button_control; public GameObject nowItem; int N; int num; int oldnum; // Start is called before the first frame update void Start() { button_control=GameObject.Find("Button_click_manager").gameObject; oldnum = 0; num = 0; N = allitem.transform.childCount; items = new GameObject[N]; for(int i=0;i< allitem.transform.childCount; i++) { items[i] = allitem.transform.GetChild(i).gameObject; } nowItem = items[num % N]; } // Update is called once per frame void Update() { if (button_control.GetComponent<click_control>().BoxRotate) { transform.Rotate(new Vector3(0, 1, 0)); button_control.GetComponent<click_control>().BoxRotate = false; } num = button_control.GetComponent<click_control>().click_num; if (oldnum != num) { items[oldnum % N].SetActive(false); items[num % N].SetActive(true); nowItem = items[num % N]; oldnum = num; } if (button_control.GetComponent<click_control>().isPlay) { items[num % N].GetComponent<AudioSource>().Play(0); GameObject.Find("Button_click_manager").gameObject.GetComponent<click_control>().isPlay = false; GameObject.Find("Button_click_manager").gameObject.GetComponent<click_control>().playIsOk(); } } }