unity 模型加点击事件

1.相机增加如下组件

unity 模型加点击事件_第1张图片

2.场景内增加EventSystem

unity 模型加点击事件_第2张图片 

3.选择需要添加点击事件的模型,添加脚本以及Event Trigger;在Event Trigger 内点击加号,增加Pointer Click ,选择脚本内容写好的点击事件方法以及选择当前模型。

unity 模型加点击事件_第3张图片 

4.脚本如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// 
/// 门操作
/// 
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;              
        }
    }


}

 

你可能感兴趣的:(unity,unity,游戏引擎)