1.相机增加如下组件
2.场景内增加EventSystem
3.选择需要添加点击事件的模型,添加脚本以及Event Trigger;在Event Trigger 内点击加号,增加Pointer Click ,选择脚本内容写好的点击事件方法以及选择当前模型。
4.脚本如下文章来源:https://www.toymoban.com/news/detail-516758.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 门操作
/// </summary>
public class DoorUtil : MonoBehaviour
{ // 模型
public Transform door_model;
public bool in_open =false;//是否向内开,默认外开
// 旋转速度
public static float rotateSpeed = 10f; //一定要使用static,不然可能变量值读不到
public static float rotateLerp = 18;
private Quaternion rotation;
private bool is_open = false;
private bool is_first = false;
// Start is called before the first frame update
void Start()
{
// 初始位置是模型
rotation = door_model.rotation;
//float rx = door_model.localEulerAngles.x;
//float rz = door_model.localEulerAngles.z;
//transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(rx, 0, rz + (in_open ? 90 : -90))), Time.deltaTime);
//this.is_open = true;
Debug.Log("Start" + door_model.localEulerAngles.z);
}
private void Update()
{
}
public void OpenOrCloseDoor()
{
if (!this.is_open)
{
float rx = transform.localEulerAngles.x;
float rz = transform.localEulerAngles.z;
float trz = rz + (this.in_open ? 90 : -90);
Debug.Log("OpenDoor"+ rx+":"+rz+":"+ trz);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(new Vector3(rx, 0, trz)), Time.time);
this.is_open = true;
}
else
{
float rx = transform.localEulerAngles.x;
float rz = transform.localEulerAngles.z;
float trz = rz + (this.in_open ? -90 : 90);
Debug.Log("CloseDoor" + rx + ":" + rz + ":" + trz);
transform.rotation = rotation;
this.is_open = false;
}
}
}
文章来源地址https://www.toymoban.com/news/detail-516758.html
到了这里,关于unity 模型加点击事件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!