Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一问题从GameObject中的另一个脚本激活相机中带有参数的函数_C#_Function_Unity3d_Call - Fatal编程技术网

C# 统一问题从GameObject中的另一个脚本激活相机中带有参数的函数

C# 统一问题从GameObject中的另一个脚本激活相机中带有参数的函数,c#,function,unity3d,call,C#,Function,Unity3d,Call,我有一个脚本,通过按下一个按钮使相机抖动,因为 这是一个公共访问功能,如果我在放置按钮时这样做,效果会很好,但我无法实现的是调用该功能,以便每当我的玩家与敌人发生碰撞时,他都会进行抖动。我希望你能帮助我 摄像机中的抖动代码为: using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScreenShaker : MonoBehaviour { private

我有一个脚本,通过按下一个按钮使相机抖动,因为 这是一个公共访问功能,如果我在放置按钮时这样做,效果会很好,但我无法实现的是调用该功能,以便每当我的玩家与敌人发生碰撞时,他都会进行抖动。我希望你能帮助我

摄像机中的抖动代码为:

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

public class ScreenShaker : MonoBehaviour {

    private float shakeAmount = 0.5f;

    private float shakeTime = 0.0f;
    private Vector3 initialPosition;
    private bool isScreenShaking = false;

    void Update () {
        if(shakeTime > 0)
        {
            this.transform.position = Random.insideUnitSphere * shakeAmount + initialPosition;
            shakeTime -= Time.deltaTime;
        }
        else if(isScreenShaking)
        {
            isScreenShaking = false;
            shakeTime = 0.0f;
            this.transform.position = initialPosition;
        }
    }

    public void ScreenShakeForTime(float time)
    {
        initialPosition = this.transform.position;
        shakeTime = time;
        isScreenShaking = true;
    }
}
敌人的密码是:

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

    public class ControladorEnemigoCirculoMediano : MonoBehaviour
    {
        void OnTriggerEnter2D(Collider2D other)
        {
            if (other.tag == "Player") 
            {
            Here I don't know what to call the public void function ScreenShakeForTime (float time); 
            I already tried many things online but when my character comes into contact with the character, I don't do the shake in the camera.
            }
        }
    }

你能告诉我在敌方游戏中物体碰撞器isTrigger是否启用吗
如果未启用,则使用碰撞检测的AlliderInter2D(Collision2D other){}来创建Unity singleton,如:

class ScreenShaker
{
  public static ScreenShaker Instance {private set; get;};

  void Awake() {
    Instance = this;
  }
}
而不是从任何地方打电话,比如:

ScreenShaker.Instance.ScreenShakeForTime(2f);
这是最简单的方法,但创建标准Singelton(由您决定)可能更好。
还有唐;不要忘记在OnDestroy()上销毁它。

这不是答案。澄清问题应该在聊天中提出,或者作为评论,而不是回答。请参阅了解更多信息。是的,但我没有足够的声誉来回答评论。我做得很好。非常感谢你的帮助。这个问题是关于和的。这与我无关。