Raycast is hitting enemy even when not in contact with it?
My melee script uses raycasting in 8 directions in conjuction with my movement script to allow a player-cube to attack in 8 directions and take health off of enemies successfully. The problem is that once an enemy is hit once, then they will keep on losing health when the attack button is pressed even when they are not in contact with any of the attacking raycasts. It is completely baffling:
public EnemyHealth enemyScript;
public Vector2 speed = new Vector2 (50, 50);
private Vector2 movement;
private Rigidbody2D playerRigid;
public Transform lineStart, rightLineEnd, leftLineEnd, upLineEnd, downLineEnd, upRightLineEnd, upLeftLineEnd, downRightLineEnd, downLeftLineEnd;
RaycastHit2D whatIHit;
public bool moveRight = false, moveLeft = false, moveUp = false, moveDown = false;
void Start ()
{
playerRigid = GetComponent<Rigidbody2D> ();
}
void Update ()
{
float inputX = Input.GetAxis ("Horizontal");
float inputY = Input.GetAxis ("Vertical");
movement = new Vector2 (
speed.x * inputX,
speed.y * inputY);
if (movement.x > 0f) {
moveRight = true; moveLeft = false; moveUp = false; moveDown = false;
Debug.DrawLine (lineStart.position, rightLineEnd.position, Color.green);
}
if (movement.x < 0f) {
moveRight = false; moveLeft = true; moveUp = false; moveDown = false;
Debug.DrawLine (lineStart.position, leftLineEnd.position, Color.green);
}
if (movement.y < 0f) {
moveRight = false; moveLeft = false; moveUp = false; moveDown = true;
Debug.DrawLine (lineStart.position, downLineEnd.position, Color.green);
} if (movement.y > 0f) {
moveRight = false; moveLeft = false; moveUp = true; moveDown = false;
Debug.DrawLine (lineStart.position, upLineEnd.position, Color.green);
}
if (movement.x > 0f && movement.y > 0f) {
moveRight = true; moveLeft = false; moveUp = true; moveDown = false;
Debug.DrawLine (lineStart.position, upRightLineEnd.position, Color.green);
}
if (movement.x < 0f && movement.y > 0f) {
moveRight = false; moveLeft = true; moveUp = true; moveDown = false;
Debug.DrawLine (lineStart.position, upLeftLineEnd.position, Color.green);
}
if (movement.x > 0f && movement.y < 0f) {
moveRight = true; moveLeft = false; moveUp = false; moveDown = true;
Debug.DrawLine (lineStart.position, downRightLineEnd.position, Color.green);
}
if (movement.x < 0f && movement.y < 0f) {
moveRight = false; moveLeft = true; moveUp = false; moveDown = true;
Debug.DrawLine (lineStart.position, downLeftLineEnd.position, Color.green);
}
Debug.DrawLine (blockRight1.position, blockRight2.position, Color.green);
Debug.DrawLine (blockLeft1.position, blockLeft2.position, Color.green);
playerRigid.velocity = movement;
Raycasting ();
RaycastUse ();
}
void FixedUpdate ()
{
playerRigid.velocity = movement;
}
void Raycasting()
{
if (Physics2D.Linecast (lineStart.position, rightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveRight == true) {
whatIHit = Physics2D.Linecast (lineStart.position, rightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
moveRight = false;
}
else if (Physics2D.Linecast (lineStart.position, leftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveLeft == true) {
whatIHit = Physics2D.Linecast (lineStart.position, leftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
moveLeft = false;
}
else if (Physics2D.Linecast (lineStart.position, upLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveUp == true) {
whatIHit = Physics2D.Linecast (lineStart.position, upLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
}
else if (Physics2D.Linecast (lineStart.position, downLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveDown == true) {
whatIHit = Physics2D.Linecast (lineStart.position, downLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
}
else if (Physics2D.Linecast (lineStart.position, upRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveUp == true && moveRight == true) {
whatIHit = Physics2D.Linecast (lineStart.position, upRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
}
else if (Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveUp == true && moveLeft == true) {
whatIHit = Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
}
else if (Physics2D.Linecast (lineStart.position, downRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveDown == true && moveRight == true) {
whatIHit = Physics2D.Linecast (lineStart.position, downRightLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
}
else if (Physics2D.Linecast (lineStart.position, upLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1")) && moveUp == true && moveLeft == true) {
whatIHit = Physics2D.Linecast (lineStart.position, downLeftLineEnd.position, 1 << LayerMask.NameToLayer ("Enemy1"));
}
}
void RaycastUse (){
if (Input.GetKeyDown (KeyCode.E) && mUpgrade1 == false) {
whatIHit.collider.gameObject.GetComponent<EnemyHealth>().DamageE1();
}
}
}
I've re-wrote it a bunch of times and it always does the same thing I have no idea what I am not understanding. Can't do anything till this is solved...Really appreciate any help!
Answer by robin-theilade · Dec 12, 2015 at 05:04 PM
@simplicitydown, whatIHit
is never assigned back to default(RaycastHit2D)
so as soon as the line cast have hit something it will remember that forever.
You need to assign the whatIHit
to default(RaycastHit2D)
when none of the other cases in your Raycasting()
method is met.
As RaycastHit2D
is a struct it will never be null so I would advise you to test if whatIHit.collider
is null to avoid null reference exceptions.
Answer by simplicitydown · Dec 12, 2015 at 06:32 PM
@robin.theilade Awesome! Well I got it to work just using
} else {
whatIHit = default(RaycastHit2D);
}
At the end of the Raycasting method.