i use unity’s FPS controller.i manage to make camera shake on the other players.
the idea is. i shoot, my camera shakes. and when enemy is hit, his camera shakes as well. like feeling the impact.
so i manage to do this. i use allbuffered. but the problem is, everyone who are not hit are affected as well. i was confused. i manage to do this with damage health script without any problem. (only the target is affected).
but with the camera shake. it affects everyone. did i miss something or is it something with photonnetwork target?
i use raycast. and store all my hit components. and able to read parent or child.
i will show you the script. it may be dirty. was really want to manage how to fix this issue. i have the clean version one before i try to do camera shake on enemies cam.
my goal for this is, i only want the enemie’s cam to shake(who is hit by my raycast) would be the only one affect. not everyone else or yourself.
public float firerate = 0.1f;
public float cooldown = 0;
public float damage = 25f;
public float impactforce = 50f;
public float forcemultiplier = 1;
public bool focus = false;
float offsetvalue;
void Update()
{
cooldown -= Time.deltaTime;
if (Input.GetButton("Fire1"))
{
Fire();
}
if (Input.GetButtonDown("Fire2"))
{
focus = !focus;
}
}
void Fire()
{
if (focus)
{
offsetvalue = 0.02f;
}
else { offsetvalue = 0.05f; }
Vector3 offset = new Vector3(Random.Range(-offsetvalue, offsetvalue), Random.Range(-offsetvalue, offsetvalue), 0);
if (cooldown > 0) return;
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward + offset);
Transform hitTransform; //name of object you hit
Vector3 hitPoints; //location of object hit
hitTransform = FindCloseshit(ray, out hitPoints);
if (hitTransform != null)
{
Health h = hitTransform.GetComponent<Health>(); //Health is the script name
while (h == null && hitTransform.parent)
{
//use in case if object is in a parent and has the health and collider
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent<Health>();
}
if (h != null)
{
// h.TakeDamage(damage);
PhotonView pv = h.GetComponent<PhotonView>();
if (pv == null)
{
Debug.LogError("no photonview detected");
}
else
{
h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, damage);
}
}
if (hitTransform.childCount > 0)
{
var childobject = hitTransform.GetChild(0).GetChild(0);
PhotonView pv = childobject.GetComponent<PhotonView>();
camerashake CS = childobject.GetComponent<camerashake>();
if (pv == null)
{
Debug.LogError("no photonview detected");
}
else
{
CS.GetComponent<PhotonView>().RPC("cameraShake", PhotonTargets.AllBuffered);
}
}
}
cooldown = firerate;
}
Transform FindCloseshit(Ray ray, out Vector3 hitPoint)
{
RaycastHit[] hits = Physics.RaycastAll(ray);
Transform closesHit = null;
float distance = 0;
float maxdistance = 2f;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits)
{
Debug.DrawRay(hit.point, hitPoint);
if (hit.transform != this.transform && (closesHit == null || hit.distance < distance)) //remove maxdistance if its long range
//&& hit.distance < maxdistance
{
// we haved hit something that is:
// 1.) not us
// 2.) the first thing we hit(that is not us)
// 3.) or, if not b, is at least closer than the previous closest hit
closesHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
Rigidbody pushforce = closesHit.GetComponent<Rigidbody>();
if (pushforce != null)
{
pushforce.AddForce(-hit.normal * (impactforce * forcemultiplier));
}
}
}
return closesHit;
}