Raycasts2D hitting colliders with tags scripting

Updated on February 11, 2019 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on February 10, 2019

I am trying to teach myself game development and coding with unity. To do this I am making a simple 2d platformer. I have been studying tutorials by unity, Brackeys, Sykoo, and blackthorprod. Using code from Blackthorprod’s patroling 2d ai tutorial i made a Trex enemy that wont fall off a cliff. Next I added another version of the same script that I modified to make it so the Trex will turn around if it hits a wall. This script worked great except for one problem; when ever my player walks in front of it, the Trex detects his collider and runs away ( not befitting a massive predator). I would like to make it so any collider tagged “Player” will not set off the Raycast. I will provide a picture of my script to help. Any advice is much appreciated. 

 
 
 
Blackthornprod’s basic dont fall script
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class DoNotFall : MonoBehaviour
{
    public float speed;
    public float distance;
    private bool x = true;
    public Transform groundDetection;
 
 
    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector2.left * speed * Time.deltaTime);
 
        RaycastHit2D groundInfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance);
        if(groundInfo.collider == false)
        {
            if(x == true)
            {
                transform.eulerAngles = new Vector3(0, -180, 0);
                x = false;
            } else
            {
                transform.eulerAngles = new Vector3(0, 0, 0);
                x = true;
            }
        }
    }
}
 
 
My modifiyed script for not crashing into walls that I want to have not react to the tag player
 
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class DoNotCrash : MonoBehaviour
{
   
    public float distance;
    private bool x = true;
    public Transform wallDetection;
    
 
    // Update is called once per frame
    void Update()
    {
        
 
        RaycastHit2D groundInfo = Physics2D.Raycast(wallDetection.position, Vector2.left, distance);
        if(groundInfo.collider == true)
        {
            if (x == true)
            {
             transform.eulerAngles = new Vector3(0, -180, 0);
             x = false;
            }
            else
            {
             transform.eulerAngles = new Vector3(0, 0, 0);
             x = true;
            } 
        }
    }
}
  • Liked by
Reply
3 on February 11, 2019

 

I would like to make it so any collider tagged “Player” will not set off the Raycast.From Zekester

So do that then. Just check if the ray hits a player, and if it does, ignore it.

 

try putting this right after your raycast.

if(groundInfo.collider.gameObject.CompareTag("Player")) return;
on February 11, 2019

Thanks for your help. I hate to bother you again but when I used this line of code I got this error:

NullReferenceException: Object reference not set to an instance of an object
DoNotCrash.Update () (at Assets/Scenes/DoNotCrash.cs:19)

It very well could just be negligence on my part, but can you help me with this too.

thanks for any time you put in.

this is my new code and line throwing the error is in bold:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoNotCrash : MonoBehaviour
{

public float distance;
private bool x = true;
public Transform wallDetection;

// Update is called once per frame
void Update()
{

RaycastHit2D groundInfo = Physics2D.Raycast(wallDetection.position, Vector2.left, distance);
if (groundInfo.collider.gameObject.CompareTag(“Player”)) return; 
if(groundInfo.collider == true)
{
if (x == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
x = false;
}
else
{
transform.eulerAngles = new Vector3(0, 0, 0);
x = true;
}
}
}
}

Wise
on February 11, 2019

My bad, I though your raycast was an if statement too.

Move it 1 line down to be after “if(groundInfo.collider == true)”, but before “if (x == true)”

RaycastHit2D groundInfo = Physics2D.Raycast(wallDetection.position, Vector2.left, distance);
if(groundInfo.collider == true)
{
if (groundInfo.collider.gameObject.CompareTag(“Player”)) return; 
else if (x == true)
{
transform.eulerAngles = new Vector3(0, -180, 0);
x = false;
}
on February 11, 2019

Your awesome! Thanks so much! Have a great day.

Show more replies
  • Liked by
Reply
Cancel