简体   繁体   English

Unity 2D Box Collider调用并运行C#脚本

[英]Unity 2D box collider to call and run C# script

I have a camera shake C# script that I want to run once the player triggers a box collider? 我有一个相机抖动C#脚本,一旦播放器触发了盒对撞机,我想运行它。 The camera shake code: 相机抖动代码:

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour
{
    public Transform camTransform;

    public float shake = 0f;

    public float shakeAmount = 0.7f;

    Vector3 originalPos;

    void Awake()
    {
        if (camTransform == null)
        {
            camTransform = GetComponent(typeof(Transform)) as Transform;
        }
    }

    void OnEnable()
    {
        originalPos = camTransform.localPosition;
    }

    void Update()
}

That's because you did not add the OnTriggerEnter , OnTriggerStay or OnTriggerExit ( depending of what you want to achieve ) function to the script. 那是因为您没有在OnTriggerEnter添加OnTriggerEnterOnTriggerStayOnTriggerExit (取决于要实现的功能)功能。

For eg: 例如:

void OnTriggerEnter(Collider other){
  if(other.tag == "Player"){
    // shake the camera here..
  }
}

Don't forget to check the trigger box in the box collider. 不要忘记在对撞机中选中trigger框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM