unity 模型加点击事件

1.相机增加如下组件

2.场景内增加EventSystem

 

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

 

4.脚本如下

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;              
        }
    }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不冷大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值