萌新求指教Ontrigg中调用Keydown有时无响应
unity3d吧
全部回复
仅看楼主
level 2
紫水晶84 楼主
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Opendoor : MonoBehaviour
{
// Start is called before the first frame update
//public Animator animator;
public Animation animation;
public GameObject Canve;
//public Collider Solid;
void Start()
{
Canve.SetActive(false);
}
void OnTriggerEnter (Collider Solid)
{
Debug.Log("Enter");
//animator.StartPlayback();
Debug.Log("ing");
Canve.SetActive(true);
//if (Input.GetKeyDown(KeyCode.E))
//{
// Debug.Log(Input.GetKeyDown(KeyCode.E));
// animation.Play();
//}
}
void OnTriggerStay (Collider Solid)
{
//animator.StartPlayback();
Debug.Log("Stay");
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log(Input.GetKeyDown(KeyCode.E));
animation.Play();
}
}
void OnTriggerExit (Collider Solid)
{
Canve.SetActive(false);
Debug.Log("Exct");
}
//void OnCollisionEnter (Collision Solid) 试探究Collision Enter和Trigger Enter的区别
//{
// //animator.StartPlayback();
// Debug.Log("ing");
// animation.Play();
//}
}
2023年11月16日 09点11分 1
level 2
紫水晶84 楼主
有点乱,整理一下,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Opendoor : MonoBehaviour
{
public Animation animation;// 想要通过按键触发的动画
public GameObject Canve //能使用按键触发时,游戏界面弹出的提示消息 canve
void Start()
{
Canve.SetActive(false);//关闭提示消息canve
}
void OnTriggerEnter (Collider Solid) //脚本挂在在角色上,当他进入指定碰撞区域Solid时
{
Debug.Log("Enter")
Canve.SetActive(true); //弹出提示
}
void OnTriggerStay (Collider Solid)
Debug.Log("Stay");
if (Input.GetKeyDown(KeyCode.E)) //监听键盘按下E键交互
{
Debug.Log(Input.GetKeyDown(KeyCode.E));
animation.Play()// 播放动画
}
}
void OnTriggerExit (Collider Solid)
{
Canve.SetActive(false);//退出区域时停用画板
Debug.Log("Exct");
}
}
2023年11月16日 09点11分 2
level 2
紫水晶84 楼主
网上找了好久没有看到类似的问题,希望万能的网友出手救救孩子吧,孩子毕设要挂了
https://www.bilibili.com/video/BV1ZH4y1i7or/ 贴吧好像贴不了视频,贴个链接在这里
2023年11月16日 10点11分 3
level 15
生命周期的问题,建议不要写在Stay里面,输入判断最好写在Update里面
2023年11月16日 10点11分 4
@紫水晶84 不要怕多,你只是做个毕设而已,不要在意那么多,在代码里面写一个Bool值,enter就设为true,exit就设为false,然后再update里面判断这个bool值,为true的话就判断输入就完事儿
2023年11月16日 12点11分
感谢感谢[哈哈],我去研究一下怎么写update里面,一开始想写在stay里面主要是考虑了一下后期类似的需要检测的区域太多了, 怕单独给每个区域挂脚本太吃性能了
2023年11月16日 12点11分
@PS🔥 嗯嗯好的大佬,我刚刚就是这么改完的,运行起来还行。
2023年11月16日 13点11分
level 5
直接调用按键对应的事件不就行了,干嘛非得走一遍…, 按键写在update里,
2023年11月16日 12点11分 5
如果直接调用按键对应的事情event的话,一个按键好像就只能对应一个一个事件了。之后的应用场景主要是让一个按键作为一个交互键,可能不能直接采用这个方法
2023年11月16日 12点11分
level 2
紫水晶84 楼主
OK啦,之前那个物体就算解决了吧。
下一步是柔性物体的拾取了,比如拾取线缆,拾取绳子,努力奋斗[酷]
2023年11月16日 13点11分 6
1