簡體   English   中英

Unity 2D Box Collider調用並運行C#腳本

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

我有一個相機抖動C#腳本,一旦播放器觸發了盒對撞機,我想運行它。 相機抖動代碼:

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()
}

那是因為您沒有在OnTriggerEnter添加OnTriggerEnterOnTriggerStayOnTriggerExit (取決於要實現的功能)功能。

例如:

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

不要忘記在對撞機中選中trigger框。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM