Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by jonhayward88 · Mar 05, 2014 at 02:10 AM · 2draycasthitgetmousebutton

How to create invisible walls in a 2d environment

Hi guys

This is my first time making a game so forgive me if I'm asking really easy questions. I've put together the basics of a top down 2d scrolling game for mobile devices. I'de like the player to be able to move in the centre of the phone but the sides are going to have walls for a background so moving in those areas would look really weird. I've tried adding cubes with box colliders but the character just pushes them out of the way or goes straight through I'm sure I'm missing something in the code and would really appreciate some help with this one.

Thanks

this is the code I'm using to move the character:

 function Update()
 {
     if(Input.GetMouseButton(0))
     {
         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if (Physics.Raycast (ray, rayCastHit))
         {
             transform.position.x = rayCastHit.point.x;
             
         }
     
     }
 
 }
Comment
Add comment · Show 8
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 emisaur · Mar 05, 2014 at 02:14 AM 0
Share

You can disable or remove the mesh renderer component to make it invisible. If the character is pushing the cube it is likely because you need to check Is Trigger to true in your collider.

avatar image jonhayward88 · Mar 05, 2014 at 02:23 AM 0
Share

I checked 'is Trigger' and unchecked '$$anonymous$$esh Renderer' on the inspector of the cube but the character just goes straight through it :/ any other suggestions?

avatar image emisaur · Mar 05, 2014 at 02:26 AM 0
Share

You'll have to attach a script which uses OnTriggerEnter and then stops the character from moving into it. I highly recommend watching the tutorial projects on the Unity site which covers this

avatar image jonhayward88 · Mar 05, 2014 at 02:29 AM 0
Share

thanks will do if i make any progress ill get back to you

thanks again for your help

avatar image jonhayward88 · Mar 05, 2014 at 02:45 AM 0
Share

I read a little about OnTriggerEnter and I started thinking about how I could implement it in my code and I got thinking about the raycast part of the code do you know if there is any way to kind of constrain where rays can go?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mulldor · Mar 05, 2014 at 04:57 AM

I had the same problem when working in 3D. Are you moving your character with a rigidbody? If so, try to use alternate means for it, such as a standard character controller.

And you can use RayCasting with layersmasks.

I.e "int wallLayer = 1 << 9" declare the 9th layer in the layer list. Then you have to invert the mask to only trigger on the 9th layer:

wallLayer = ~wallLayer

Then in you use your rayCast with your wallLayer as your layermask

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 jonhayward88 · Mar 05, 2014 at 07:06 PM 0
Share

thanks guys for all your help after ages of going through tutorials and testing out multiple workarounds and getting nowhere (obviously myself at fault not the workarounds) I managed to put together a really basic botch-job. I basically added another camera that the ray casts would be sent from and made the area of that a lot smaller than the play area and then changed the code. I'll probably have to change this pretty quick but then hopefully ill understand layers and ontriggerenter a lot better and hopefully wont have too much trouble.

thanks again guys

avatar image
0

Answer by Vitorcampea · Mar 05, 2014 at 07:41 PM

Hello,

If i undertood what do you want, invisible walls, when your player hit them he stop right?

First you will need your player to have Colider2D, same as the walls

For a player to able to move those invisible walls, means that they have RigidBody

  • Set walls "Mass" to 999, so they can be very heavy

  • Set walls Gravity to 0, so they wont fall down :P

  • Set player "Mass" to 0.01, so he cant push nothing, he will be very light

  • Set walls Gravity to 0, so he wont fall down

  • To turn them invisible just remove Sprite Renderer

  • To give them Colider Shape, you can use BoxCollider2D and change size, you will see some green line ;)

I hope i helped from what i undertood, good luck have a nice day

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Physics raycast hit offset from where it should be 1 Answer

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

How to find out in 2d the distance betwen beginning of the 2d raycast and the point of meeting with 2d collider. 2 Answers

2D Bouncing formula doesn't work properly 2 Answers

2D Animation does not start 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