How to limit this particular raycast distance?
I need to make the rays distance 2 if the playerpref Wep = 1. I have tried but i have failed alot please help me!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerShootingAss : MonoBehaviour {
public float fireRate = 0.5f;
public float cooldown = 0;
public float cooldown2 = 0;
public float damage = 25;
FXManager fxManager;
private int Wep;
public bool cooldownChange = false;
public bool cooldownChange2 = false;
public Image reloaddefault;
public Image reloadsniper;
void Start(){
fxManager = GameObject.FindObjectOfType<FXManager>();
if (fxManager == null) {
Debug.LogError ("couldn't find an fx manager");
}
reloaddefault = transform.FindChild("FirstPersonCharacter").FindChild ("Laserbow").FindChild ("PlayerCanvas").FindChild ("HealthBGgun1").FindChild ("Health").GetComponent<Image> ();
reloadsniper = transform.FindChild("FirstPersonCharacter").FindChild ("Sword").FindChild ("PlayerCanvas").FindChild ("HealthBGgun2").FindChild ("Health").GetComponent<Image> ();
}
void Update () {
Wep = PlayerPrefs.GetInt ("Wep");
cooldown -= Time.deltaTime;
cooldown2 -= Time.deltaTime;
reloadsniper.fillAmount = (float)cooldown2 / (float)fireRate;
reloaddefault.fillAmount = (float)cooldown / (float)fireRate;
if (Input.GetButton ("Fire2")) {
transform.FindChild ("FirstPersonCharacter").FindChild ("PlayerCanvas").FindChild ("HealthBG").gameObject.SetActive(false);
transform.FindChild ("FirstPersonCharacter").FindChild ("crosshair").gameObject.SetActive (false);
} else {
transform.FindChild ("FirstPersonCharacter").FindChild ("PlayerCanvas").FindChild ("HealthBG").gameObject.SetActive(true);
transform.FindChild ("FirstPersonCharacter").FindChild ("crosshair").gameObject.SetActive (true);
}
if (Input.GetButton ("Fire1")) {
Fire ();
}
if (Wep == 0) {
fireRate = 3f;
damage = 100;
} else {
fireRate = 1f;
damage = 50;
}
if (Input.GetButtonDown("Wep1")) {
cooldownChange = true;
}
if (Input.GetButtonDown("Wep2")) {
cooldownChange2 = true;
}
if (cooldownChange == true){
cooldownChange = false;
}
if (cooldownChange2 == true){
cooldownChange2 = false;
}
}
void Fire(){
if (Wep == 0) {
if (cooldown > 0) {
return;
}
} else {
if (cooldown2 > 0) {
return;
}
}
Debug.Log ("We shot");
Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
Transform hitTransform;
Vector3 hitPoints;
hitTransform = FindClosestHitObject (ray, out hitPoints);
if (hitTransform != null) {
Debug.Log ("We hit: " + hitTransform.name);
//hitInfo.point
Health h = hitTransform.GetComponent<Health> ();
while (h == null && hitTransform.parent) {
hitTransform = hitTransform.parent;
h = hitTransform.GetComponent<Health> ();
}
if (h != null) {
h.GetComponent<PhotonView> ().RPC ("TakeDamage", PhotonTargets.All, damage);
}
if (fxManager != null) {
DoGunFX (hitPoints);
}
} else {
if (fxManager != null) {
DoGunFX (hitPoints);
WeaponData wd = gameObject.GetComponentInChildren<WeaponData> ();
hitPoints = wd.transform.position + (wd.transform.forward*100);
}
}
if (Wep == 0) {
cooldown = fireRate;
} else {
cooldown2 = fireRate;
}
}
void DoGunFX(Vector3 hitPoints){
WeaponData wd = gameObject.GetComponentInChildren<WeaponData> ();
fxManager.GetComponent<PhotonView> ().RPC ("SniperBulletFX", PhotonTargets.All, wd.transform.position, hitPoints);
}
Transform FindClosestHitObject(Ray ray, out Vector3 hitPoints) {
RaycastHit[] hits = Physics.RaycastAll (ray);
Transform closestHit = null;
float distance = 0;
hitPoints = Vector3.zero;
foreach (RaycastHit hit in hits) {
if (hit.transform != this.transform && (closestHit == null || hit.distance < distance)) {
closestHit = hit.transform;
distance = hit.distance;
hitPoints = hit.point;
}
}
return closestHit;
}
}
Answer by SPOTNINJADUD7890 · Oct 22, 2016 at 02:35 PM
Fixed final code
RaycastHit[] hits = Physics.RaycastAll (ray); if (Wep == 0) { hits = Physics.RaycastAll (ray); } else { hits = Physics.RaycastAll (ray, 2f); }
Answer by b1gry4n · Oct 21, 2016 at 03:41 AM
It looks like you are getting your ray using the camera transform and that transforms "forward". The value returned will be a Vector3 where each value is between 0 and 1. Drawing that ray, it will always be 1 unit long. To make it 2 units long, multiply it by 2.0
if (Wep == 1) {
Vector3 val = Camera.main.transform.forward*2.0f
ray = new Ray (Camera.main.transform.position,val );
}
this does not work giving me the error Assets/scripts/PlayerShootingAss.cs(86,25): error CS0019: Operator *=' cannot be applied to operands of type
UnityEngine.Ray' and `float'
also how would i change the value back to the infinite range when i switch back?
seems like it isnt working it is still shooting the full distance...
You want the object shot to be limited in distance or you want the ray to cast a specific distance? The question says
How to limit this particular raycast distance?
if you want a raycast to shoot a specific distance I suggest you rework your code.
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2.0f, mask){
//hit something
Debug.Log(hit.transform.gameObject.name);
}
where "2.0f" would be the length of the cast ray.
I dont know were to put this. lol please tell me
Answer by Bunny83 · Oct 21, 2016 at 11:02 PM
The length of the Ray direction is not relevant. The Physics.RaycastAll method has an optional parameter where you can specify the max distance. If you don't pass any distance (like you did) the ray will have infinite length.
i still dont understand were i would put this in my code i cannot find raycast all.
could you tell me were to put this i cannot find Physics.RaycastAll in my code
Your answer
Follow this Question
Related Questions
Photon Networking - How to move all players from game scene to current room scene? 0 Answers
Unity 5 - My raycast is hitting my player, and I can't figure out why 2 Answers
UNET Clients Laggy 0 Answers
Both players reach same time how to get result draw in photon unity 1 Answer
Raycast over Photon 0 Answers