Question by
KeskitaloStudios · Jun 12, 2016 at 04:32 AM ·
raycastshootingraycastingrangelength
How to make raycast range on this script?
using UnityEngine; using System.Collections;
public class PlayerShooting : MonoBehaviour {
public string weaponname = "PickAxe";
public float weapondamage = 10f;
public float weaponrange = 1f;
public float cooldown = 0f;
public float firerate = 1f;
FXManager fxmanager;
[SerializeField]
private Camera cam;
void Start()
{
fxmanager = GameObject.FindObjectOfType<FXManager>();
if (fxmanager == null)
{
Debug.LogError("Cant find FX Manager");
}
}
void Update()
{
cooldown -= Time.deltaTime;
if (Input.GetButton("Fire1"))
{
Fire();
}
}
void Fire()
{
if (cooldown > 0)
{
return;
}
Ray ray = new Ray(cam.transform.position, cam.transform.forward);
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject(ray, out hitPoint);
if (hitTransform != null)
{
Debug.Log("We hit: " + hitTransform.name);
Health h = hitTransform.GetComponent<Health>();
while (h == null && hitTransform.parent)
{
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent<Health>();
}
if (h != null)
{
PhotonView pview = h.GetComponent<PhotonView>();
if (pview == null)
{
Debug.LogError("NO PHOTONVIEW ATTACHED");
}
else
{
GroupMember GMember = hitTransform.GetComponent<GroupMember>();
GroupMember myGMember = this.GetComponent<GroupMember>();
if (GMember == null || GMember.teamID == 0 || myGMember == null || myGMember.teamID == 0 || GMember.teamID != myGMember.teamID)
{
h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, weapondamage);
}
}
}
if (fxmanager != null)
{
fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
}
}
else
{
if (fxmanager != null)
{
hitPoint = cam.transform.position + (cam.transform.forward * weaponrange);
fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
}
}
cooldown = firerate;
}
Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint)
{
RaycastHit[] hits = Physics.RaycastAll(ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits)
{
if (hit.transform != this.transform && (closestHit == null || hit.distance < distance))
{
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}
}
Comment
Fixed! dropping off the code that i used to fix it if someone else needs it :) void Fire() { if (cooldown > 0) { return; }
Ray ray = cam.ScreenPointToRay(Input.mousePosition); //these are the changes made
RaycastHit hit;//these are the changes made
if (Physics.Raycast(ray, out hit, weaponrange))//these are the changes made
{
Debug.DrawLine(ray.origin, hit.point);//these are the changes made
}
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject(ray, out hitPoint);
if (hitTransform != null)
{
Debug.Log("We hit: " + hitTransform.name);
Health h = hitTransform.GetComponent<Health>();
while (h == null && hitTransform.parent)
{
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent<Health>();
}
if (h != null)
{
PhotonView pview = h.GetComponent<PhotonView>();
if (pview == null)
{
Debug.LogError("NO PHOTONVIEW ATTACHED");
}
else
{
Group$$anonymous$$ember G$$anonymous$$ember = hitTransform.GetComponent<Group$$anonymous$$ember>();
Group$$anonymous$$ember myG$$anonymous$$ember = this.GetComponent<Group$$anonymous$$ember>();
if (G$$anonymous$$ember == null || G$$anonymous$$ember.$$anonymous$$mID == 0 || myG$$anonymous$$ember == null || myG$$anonymous$$ember.$$anonymous$$mID == 0 || G$$anonymous$$ember.$$anonymous$$mID != myG$$anonymous$$ember.$$anonymous$$mID)
{
h.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.AllBuffered, weapondamage);
}
}
}
if (fxmanager != null)
{
fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
}
}
else
{
if (fxmanager != null)
{
hitPoint = cam.transform.position + (cam.transform.forward * 100);
fxmanager.GetComponent<PhotonView>().RPC("PickAxeAirFX", PhotonTargets.All, cam.transform.position, hitPoint);
}
}
cooldown = firerate;
}
Transform FindClosestHitObject(Ray ray, out Vector3 hitPoint)
{
RaycastHit[] hits = Physics.RaycastAll(ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits)
{
if (hit.transform != this.transform && (closestHit == null || hit.distance < distance))
{
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}