! Laser knockback don't work ? :( !
Hello, please try to help me.
I have a 2D game where I want to add a Laser.
So here I have a LineRenderer2D with two Debug.DrawLine which detects the Player to know from what direction player is going to know where to push knockback on him.
The problem here is that knockBack works only from One DrawLine, and the second one is not responding, Debug mode it shows that it works but in the game is not so.
What can do such a problem? Am I doing something wrong?
Here is the script:
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) {
player.GetComponent<PlayerControllerScript> ().laserHit = true;
player.GetComponent<PlayerControllerScript> ().knockFromRight = true;
Debug.Log ("Right");
} else {
player.GetComponent<PlayerControllerScript> ().laserHit = false;
}
if (spottedLeft == true) {
player.GetComponent<PlayerControllerScript> ().laserHit = true;
player.GetComponent<PlayerControllerScript> ().knockFromRight = false;
Debug.Log ("Left");
} else {
player.GetComponent<PlayerControllerScript> ().laserHit = false;
}
}
}
}
@Harinezumi We a worked on this issue sometime before here: unity question link
Can we push this to the resolution? :)
Your answer
Follow this Question
Related Questions
2D Knockback problem 2 Answers
How to make my gun do damge and destroy a object? 1 Answer
2D moving Script 1 Answer
[SOLVED] Problems creating a Sprite through script 0 Answers
My knockback script is not working 0 Answers