如何使这个提取代码正常工作?

huangapple go评论70阅读模式
英文:

How can I make this pick-up code work properly?

问题

这里有一个我从YouTube教程中找到的脚本。在游戏中它有时候能用,但我不明白为什么。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUp : Interactable
{
    public GameObject FlashLightOnPlayer;

    void Start()
    {
        FlashLightOnPlayer.SetActive(false);
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            if (Input.GetKey(KeyCode.E))
            {
                this.gameObject.SetActive(false);
                FlashLightOnPlayer.SetActive(true);
            }
        }
    }
}

这是我有的两个对象。Flashlight(1)是Player的子对象。

Flashlight
Flashlight(1)

我尝试按照这个教程:https://youtu.be/zEfahR66Pa8,有时候拾取正常,有时候不行。我该怎么办?

英文:

I have this script here that I took from a youtube tutorial. It only works sometimes in game and I don`t understand why.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUp : Interactable
{
    public GameObject FlashLightOnPlayer;
    // Start is called before the first frame update
    void Start()
    {
        FlashLightOnPlayer.SetActive(false);
    }
    
    
   private void OnTriggerStay(Collider other) 
   { 
    if (other.gameObject.tag == "Player")
    {
        if(Input.GetKey(KeyCode.E))
        {
        this.gameObject.SetActive(false);
        FlashLightOnPlayer.SetActive(true);
        }
   }
  }
}  

These are the two objects that I have. Flashlight(1) is a child of Player.

Flashlight
Flashlight(1)

I tried following this tutorial here : https://youtu.be/zEfahR66Pa8
Sometimes when I try and pick it up, it works, sometimes it doesn`t.
What can I do?

答案1

得分: 1

https://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html 中:

> OnTriggerStay 几乎在每一帧调用,用于与触发器接触的几乎所有其他碰撞体。该函数在物理计时器上运行,因此不一定每一帧都会执行。

使用 Input.GetKey() 时,希望在 Update() 中的每一帧调用它,以确保不会错过任何按键。在像 OnTriggerStay() 这样基于物理的东西中调用 Input.GetKey() 是有风险的,因为 OnTriggerStay() 在独立的物理帧速率上运行。我尝试查找是否有人在 Update() 之外使用 Input.GetKey(),但迄今为止我没有找到。你最好的选择是:

  1. OnTriggerStay() 中创建一个标志,并在 Update() 中检查该标志,或
  2. Update() 中创建一个标志,并在 OnTriggerStay() 中检查该标志
英文:

From https://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html:

> OnTriggerStay is called on almost all the frames for every Collider other that is touching the trigger. The function is on the physics timer so it won't necessarily run every frame.

With Input.GetKey(), you want to call it every frame in an Update() to not miss any key press. Calling Input.GetKey() inside something physic-based like OnTriggerStay() is risky as OnTriggerStay() run on an independent physic framerate. I've tried to find if there is anyone who use Input.GetKey() outside Update(), but so far I've found none. Your best bet is:

  1. Creating a flag in OnTriggerStay() and use Update() to check that flag, or
  2. Creating a flag in Update() and use OnTriggerStay() to check that flag.

huangapple
  • 本文由 发表于 2023年3月9日 20:49:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75684852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定