Vfx on impact of a raycast??

Options
Hi,

I've got a working shooting script that works with a health script on each character so that players loose health points when hit by the raycast, and if you shoot at the water in my scene there's a water splash effect that instantiates where the ray hits, but the water splash only occurs locally and is not visible to other players. What do I need to do to my code specifically, to make the instantiated water splash visible to all players in the scene?
The water splash prefab has a photonView script on it:
(); if (weaponData == null) { weaponData = gameObject.GetComponentInChildren<WeaponData>(); if (weaponData == null) { Debug.LogError("Did not find a WeaponData script on child object"); } return; } if (cooldown > 0) { return; } //Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward ); Ray ray = new Ray(cam.transform.position, cam.transform.forward); Transform hitTransform; Vector3 hitPoint; //if (Physics.Raycast(ray, out hitInfo)) //{ // Debug.Log("We hit: " + hitInfo.collider.name); //} hitTransform = FindClosestHitObject(ray, out hitPoint); if (hitTransform != null) { Debug.Log("We hit: " + hitTransform.name); // we could do a FX animation on the hit location // DoSmokeEffect (hitInfo.point); if (hitTransform.gameObject.tag == "waterEx") { GameObject impactGO = Instantiate(impactEffectwater, hitPoint, transform.rotation); Destroy(impactGO, 1f); }">

Comments

  • I don't know why the code tool did that, it looks strange: here's the code directly pasted:

    void Fire()
    {
    //fxManager = GameObject.FindObjectOfType();
    if (weaponData == null)
    {
    weaponData = gameObject.GetComponentInChildren();
    if (weaponData == null)
    {
    Debug.LogError("Did not find a WeaponData script on child object");
    }
    return;
    }
    if (cooldown > 0)
    {
    return;
    }
    //Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward );
    Ray ray = new Ray(cam.transform.position, cam.transform.forward);
    Transform hitTransform;
    Vector3 hitPoint;
    //if (Physics.Raycast(ray, out hitInfo))
    //{
    // Debug.Log("We hit: " + hitInfo.collider.name);
    //}

    hitTransform = FindClosestHitObject(ray, out hitPoint);
    if (hitTransform != null)
    {
    Debug.Log("We hit: " + hitTransform.name);

    // we could do a FX animation on the hit location
    // DoSmokeEffect (hitInfo.point);
    if (hitTransform.gameObject.tag == "waterEx")

    {
    GameObject impactGO = Instantiate(impactEffectwater, hitPoint, transform.rotation);
    Destroy(impactGO, 1f);
    }

    }
  • DavidSWu
    DavidSWu
    edited February 2019
    Options
    You could try:
    GameObject impactGO = Photon.Instantiate(impactEffectwater, hitPoint, transform.rotation);
    But using a synchronized object for a splash sounds expensive.
    If you are already synchronizing the shot, just simulate it on all clients and they will each produce their own splash.
  • That sounds much better but could you maybe explain how?

    The "GameObject impactGO = Photon.Instantiate(impactEffectwater, hitPoint, transform.rotation);" comes up with an error:
    Assets/Networking/PlayerShooting.cs(81,46): error CS0234: The type or namespace name `Instantiate' does not exist in the namespace `Photon'. Are you missing an assembly reference?
  • This is my shooting script:

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

    public class PlayerShooting : Photon.MonoBehaviour {

    float cooldown = 0;

    FXManager fxManager;
    WeaponData weaponData;
    public Camera cam;

    [PunRPC]
    public GameObject impactEffectwater;
    //public GameObject impactEffectripple;
    public GameObject impactEffectrock;
    public GameObject impactEffectblood;



    void Start()
    {
    fxManager = GameObject.FindObjectOfType();

    if (fxManager == null) {
    Debug.LogError("Couldn't find FXManager script");
    }


    }

    void Update () {
    if (photonView.isMine)
    {
    cooldown -= Time.deltaTime;

    if (Input.GetButton("Fire1"))
    {//Player wants to shoot so SHOOT!
    Fire();

    }
    }
    }
    [PunRPC]
    void Fire()
    {
    //fxManager = GameObject.FindObjectOfType();
    if (weaponData == null)
    {
    weaponData = gameObject.GetComponentInChildren();
    if (weaponData == null)
    {
    Debug.LogError("Did not find a WeaponData script on child object");
    }
    return;
    }
    if (cooldown > 0)
    {
    return;
    }
    //Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward );
    Ray ray = new Ray(cam.transform.position, cam.transform.forward);
    Transform hitTransform;
    Vector3 hitPoint;
    //if (Physics.Raycast(ray, out hitInfo))
    //{
    // Debug.Log("We hit: " + hitInfo.collider.name);
    //}

    hitTransform = FindClosestHitObject(ray, out hitPoint);
    if (hitTransform != null)
    {
    Debug.Log("We hit: " + hitTransform.name);

    // we could do a FX animation on the hit location
    // DoSmokeEffect (hitInfo.point);
    if (hitTransform.gameObject.tag == "waterEx")

    {
    GameObject impactGO = Instantiate(impactEffectwater, hitPoint, transform.rotation);
    Destroy(impactGO, 1f);
    }
    if (hitTransform.gameObject.tag == "rock")

    {
    GameObject impactGO = Instantiate(impactEffectrock, hitPoint, transform.rotation);
    Destroy(impactGO, 1f);
    }

    //BLOD EFFECT virker ikke fordi boon har samme hittransform og man kan kun spille med ham lige nu
    //if (hitTransform.gameObject.tag == "water1")

    //{
    // GameObject impactGO3 = Instantiate(impactEffectblood, hitPoint, transform.rotation);
    // Destroy(impactGO3, .5f);
    //}



    Health h = hitTransform.GetComponent();
    while (h == null && hitTransform.parent)
    {
    hitTransform = hitTransform.parent;
    h = hitTransform.GetComponent();
    }



    if (h != null)
    {
    //h.TakeDamage(damage);


    //The RPC (remote calls a function) and shares it with the network. In this case "All" players in the game get the updated parameter: damage - from the health script. (It updates health when and object is hit on everybodys computer)
    //h.GetComponent().RPC("TakeDamage", PhotonTargets.All, damage);

    PhotonView pv = h.GetComponent();
    if (pv == null)
    {
    Debug.LogError("Missing PhotonView script");
    }
    else
    {
    h.GetComponent().RPC("TakeDamage", PhotonTargets.AllBuffered, weaponData.damage);
    }

    }
    if(fxManager != null)
    {
    DoGunFX(hitPoint);
    }
    }
    else
    {
    //We didn't hit anything except empty space, but let's do an FX anyway.
    if (fxManager != null)
    {
    DoGunFX(hitPoint);
    hitPoint = cam.transform.position + (cam.transform.forward *100f);
    }
    }


    cooldown = weaponData.fireRate;

    }

    private GameObject Instantiate(object prefab, GameObject impactEffectwater, Vector3 hitPoint, Quaternion rotation)
    {
    throw new NotImplementedException();
    }

    void DoGunFX(Vector3 hitPoint)
    {

    WeaponData weaponData = gameObject.GetComponentInChildren();

    //fxManager.GetComponent().RPC("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint);
    fxManager.GetComponent().RPC("SniperBulletFX", PhotonTargets.All, weaponData.transform.position, hitPoint, weaponData.transform.rotation);



    }

    Transform FindClosestHitObject(Ray ray, out Vector3 hitpoint)
    {
    RaycastHit[] hits = Physics.RaycastAll(ray);

    Transform closestHit = null;
    float distance = 0f;
    hitpoint = Vector3.zero;

    foreach (RaycastHit hit in hits)
    {
    if(hit.transform != this.transform && (closestHit == null || hit.distance < distance))
    {
    //we have hit something that is not us:
    //a: not us
    //b: the first thing we hit that isn't us
    closestHit = hit.transform;
    distance = hit.distance;
    hitpoint = hit.point;
    }


    }
    //closest hit is now either null,or the closest thing that isn't us.
    return closestHit;
    }
    }