• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by Rembo4Fight · Aug 28, 2018 at 03:02 PM · scripting problemif-statementslaserelseconflict

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.

alt text

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;
             }
         }
     }
 }


screen-shot-2018-08-11-at-224557.png (226.8 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rembo4Fight · Aug 28, 2018 at 09:13 PM 0
Share

Thank you, for your reply!

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.png
screen-shot-2018-08-29-at-001014.png (88.8 kB)
avatar image JVene Rembo4Fight · Aug 28, 2018 at 11:18 PM 0
Share

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.

avatar image Rembo4Fight JVene · Aug 29, 2018 at 03:41 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

220 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Else section in if statement not executing? 0 Answers

Is there a dont load level type script 0 Answers

If statement not working. 0 Answers

Three button combination to open next scene 2 Answers

Issue with Dynamic Laser Reflection on Walls 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges