Laser System - "If" statement conflict
Okay, it is the complicated question, but I will try to explain the problem. So, I'm making 2d laser system, where is to rays from two sides are detecting the player who tries to collide with the laser.
The main problem here I think thank two "if" are conflicting, so Only One side are working and detecting the player, which is giving him damage, the not working side is just know that ray detect the player. I hope the code will not look like something hard to understand.
Here is it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OneLaserScript : MonoBehaviour {
private LineRenderer lineRenderer;
public Transform laserHit;
public GameObject player;
public Transform leftCheck, rightCheck;
public Transform leftHit, rightHit;
public bool spottedLeft, spottedRight;
public LayerMask detectionLayers;
public LayerMask playerDetection;
void Start ()
{
lineRenderer = GetComponent<LineRenderer> ();
}
void FixedUpdate ()
{
float distan = Mathf.Infinity;
RaycastHit2D hit = Physics2D.Raycast (transform.position, transform.up,distan,playerDetection); //Direction of the laser
if (hit.transform != null)
{
lineRenderer.enabled = true;
laserHit.position = hit.point;
lineRenderer.SetPosition(0, transform.position);
lineRenderer.SetPosition(1, laserHit.position);
}
else
{
lineRenderer.enabled = false;
}
if (hit.collider != null) {
laserHit.position = hit.point;
lineRenderer.SetPosition (0, transform.position);
lineRenderer.SetPosition (1, laserHit.position);
Debug.DrawLine (leftCheck.position, leftHit.position, Color.green);
spottedLeft = Physics2D.Linecast (leftCheck.position, leftHit.position, detectionLayers);
Debug.DrawLine (rightCheck.position, rightHit.position, Color.green);
spottedRight = Physics2D.Linecast (rightCheck.position, rightHit.position, detectionLayers);
if (spottedRight == true && spottedLeft == false) {
player.GetComponent<Rigidbody2D> ().AddForce(new Vector2 (-5000, 1000));
player.GetComponent<PlayerControllerScript> ().laserHit = true;
player.GetComponent<PlayerControllerScript> ().knockFromRight = true;
} else {
player.GetComponent<PlayerControllerScript> ().laserHit = false;
}
if (spottedLeft == true && spottedRight == false) {
player.GetComponent<PlayerControllerScript> ().laserHit = true;
player.GetComponent<PlayerControllerScript> ().knockFromRight = false;
} else {
player.GetComponent<PlayerControllerScript> ().laserHit = false;
}
}
}
}
Answer by JVene · Aug 28, 2018 at 07:25 PM
There is some context missing here.
I don't see, for example, where leftCheck and rightCheck are assigned values, and I don't know if this script is only on one GameObject or two GameObjects.
However, if I guess that this script applies to one and only one game object, but your text suggests there are two laser beams used for sensing collision, you are only testing one.
I get a hint from the debug.drawlines that there is only one such script. While those debug lines appear to draw two positions, I see only one raycast being tested, so I'd expect only one thing is going to collide.
If your text reference to "two if" are conflicting corresponds to lines 32 and 44 of this post, that's not exactly a problem, but it is a bit odd. I'm not sure what circumstance provides a collider but no transform, but I don't see how these two forms causes a problem, they are largely redundant except for the lineRenderer.enabled setting.
If you do have two GameObjects, and this code is attached to both, perhaps I'm incorrect here, but at the moment it seems you need two raycasts, one for each side you're detecting.
I will add here a screenshot of the laser system, to give you understand how it works(maybe not the best right way) ![alt text][1] And you think that line 32 and 44 conflicting? hit.transform and hit.collider How it works, maybe I should remove something and add the lines to another? [1]: /storage/temp/123541-screen-shot-2018-08-29-at-001014.pngThank you, for your reply!
No, I don't see that those to tests at 32 and 44 collide, just that the could be combined unless you have some reason to think you'd have a transform without a collider. As I see it, you either get both or nothing, but I'm not designing your game so I don't know.
That said, what I think is that you have only 1 raycast, so I'd expect there to be only one beam that could be detected, not two. I'd expect to see two raycasts for two separate beams.
Yes, I have only one Raycast, but it's not detecting the player, it's just visual Two Linecast are detecting the player from left and right sides on 51 and 53 lines. What are your thoughts on this issue?
Your answer
![](https://koobas.hobune.stream/wayback/20220612173643im_/https://answers.unity.com/themes/thub/images/avi.jpg)