- Home /
Problem is not reproducible or outdated
Unity2D raycast to check for a wall, Issues. ~Still not solved...Anybody?~
Hello my fellow Unity friends I have an issue.
I'm using a basic blink (teleport) ability which just directly shifts the player's transform. the problem is I have certain walls I don't want the player to be able to teleport through. I'm trying to use the below script to prevent the player when they are at a 1.5 distance from the "Barriers". This distance is the exact blink distance the player can move. Edit: This function is called in a fixed update
If any more information is needed, I have more. Thank you all in advance! :D
Edit (1/1/2018): Code now gives the error: NullReferenceException: Object reference not set to an instance of an object Blink.BarrierCheck () (at Assets/Scripts/Hope/Abilities/Blink.cs:41) Blink.FixedUpdate () (at Assets/Scripts/Hope/Abilities/Blink.cs:30)
public class Blink : MonoBehaviour
{
public bool CanBlink;
public bool Allowblink;
public Animator Anim;
public GameObject Player;
public Vector3 OldPos;
public Vector3 NewPos;
public float BlinkCooldown;
Movement movement;
public void Start()
{
CanBlink = true;
Allowblink = true;
Player = GameObject.FindGameObjectWithTag("Player");
movement = GetComponent<Movement>();
BlinkCooldown = 3.0f;
}
public void FixedUpdate()
{
BarrierCheck();
BlinkWatcher();
}
public void BarrierCheck()
{
if (GameObject.FindGameObjectWithTag("Player").GetComponent<Movement>().facingRight)
{
RaycastHit2D hit = (Physics2D.Raycast(transform.position, transform.right, 3f, (LayerMask.GetMask("Barrier"))));
Debug.DrawRay(transform.position, transform.right, Color.black);
if (hit.collider.CompareTag("Barrier"))
{
Allowblink = false;
Debug.Log("Collider was hit");
}
else
{
Debug.Log("Null ray");
}
}
else
{
RaycastHit2D hit = (Physics2D.Raycast(transform.position, -transform.right, 3f, (LayerMask.GetMask("Barrier"))));
Debug.DrawRay(transform.position, -transform.right, Color.black);
if (hit.collider.CompareTag("Barrier"))
{
Allowblink = false;
Debug.Log("Collider was hit");
}
else
{
Debug.Log("Null ray");
}
}
}
public void BlinkWatcher()
{
BlinkCooldown -= Time.deltaTime;
if (Input.GetKey(KeyCode.Mouse1) && CanBlink && Allowblink && BlinkCooldown <= 0f)
{
float x = movement.facingRight ? 3f : -3f;
Player.transform.position += new Vector3(x, 0f, 0f);
BlinkCooldown = 3.0f;
}
}
}
Is this script on the player? Why do you call Player.transform.forward like it is static variable? You can say hit.collider.CompareTag("Barrier") it is more efficient i think. Also what is the actual issue? you getting errors? Try Debug.DrawRay to see if the ray looks correct
Yes the script is attached to the player. I called it because I felt like exposing the player gameobject and taking it's transform would cause less issues if I used the script for something else later. If this might be the issue I can change it to just transform.forward. I'll try the new CompareTag but I believe the issue isn't that, as when I used debug.drawray, there didn't seem to be a visible ray. The issue is the bool isn't being changed for some reason :| Thanks for the reply, was getting worried I posted into the wrong place again. Lol
Hard to tell without seeing more of your project. Try this code maybe.
Edit: removed bad code
Answer by theterrificjd · Dec 26, 2017 at 10:36 AM
Hmm. Could be a number of things.
Maybe try changing it to a public bool BarrierCheck();
Then, instead of setting Allowblink to false, you can return false or true on whether or not they can blink. (this will only fix if your allowblink is being overridden to true somehow, and I have no idea whether that'd be true or not, just a random thought)
Also, when having troubles, it's always good to have a Debug.DrawRay mimicing the raycast in my experience. It's pointed out some very obvious problems for me in the past.
I also believe the 'tag.Equals' isn't the best way to do it nowadays. The method I learned was 'CompareTag("Tag")'
I could be wrong, and if it works, it works.
Beyond that, you can use layermasks to simplify the function, and make sure it isn't being clipped by another collider. Add the barrier to a new Layer in the inspector (Right above the Transform component) If it's your first custom layer it will be 8, so I'll assume that:
int layermask = (1 << 8); // Will only hit layer 8
int layermask = ~(1 << 8); // Will hit everything but layer 8
int layermask = ~(1 << 8 | 1 << 9); // will hit everything but layers 8 and 9
int layermask = (1 << 8 | 1 << 9); // will hit only layers 8 and 9
No other object is in the scene area that would cause the wrong collider to be being hit. I put a debug.log statement before setting AllowBlink to false, but that doesn't even get called.
Answer by Lumineux · Dec 27, 2017 at 11:38 AM
The code seems right. Make sure the tag of your barriers is correctly written.
I already deleted their tags and copied them right out of the script, no luck there
Follow this Question
Related Questions
Character "bounces" off of flat terrain 0 Answers
Bullet not destroying on contact 0 Answers
Unexpected behaviour of OnTriggerStay2D 0 Answers