Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Feb 09, 2018 at 08:34 PM by mmaalex22112 for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by mmaalex22112 · Dec 26, 2017 at 07:24 AM · unity 5unity 2dphysics2dtagphysics.raycast

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

Comment
Add comment · Show 11
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 Buckslice · Dec 26, 2017 at 07:58 AM 0
Share

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

avatar image mmaalex22112 Buckslice · Dec 26, 2017 at 08:31 AM 0
Share

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

avatar image Buckslice mmaalex22112 · Dec 26, 2017 at 09:10 AM 0
Share

Hard to tell without seeing more of your project. Try this code maybe.

Edit: removed bad code

Show more comments

2 Replies

  • Sort: 
avatar image
0

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
 
 
Comment
Add comment · Show 1 · 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 mmaalex22112 · Dec 27, 2017 at 01:53 AM 0
Share

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.

avatar image
0

Answer by Lumineux · Dec 27, 2017 at 11:38 AM

The code seems right. Make sure the tag of your barriers is correctly written.

Comment
Add comment · Show 1 · 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 mmaalex22112 · Dec 27, 2017 at 08:02 PM 0
Share

I already deleted their tags and copied them right out of the script, no luck there

Follow this Question

Answers Answers and Comments

148 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

Related Questions

Ball with bounciness 1 bounces higher and higher in unity2d,Ball jumps higher and higher when bounciness set to 1 1 Answer

Bullet not destroying on contact 0 Answers

Character "bounces" off of flat terrain 0 Answers

How to build upon Jongallant's verlet integration rope project 0 Answers

What Should i use rigidbody.AddTorque() or Quaternions ? 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