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 Nony42 · Jan 13, 2014 at 04:40 AM · raycast

Wall detection using Rays

I am making a 2.5D platformer controller and I am manipulation the player using transform.translate. I've been taking the script from this tutorial almost exactly, minus the animation portion and sliding: http://www.youtube.com/playlist?list=PLFt_AvWsXl0e-g21S-MiPArfEhlv9pUgC

  • I use a function that takes in the current speed, target speed and an acceleration to calculate how much to move the character in order to achieve acceleration and a bit of a sliding motion when changing directions.

  • I pass the amount to move the character into a variable

  • Then finally pass that variable into another function that detects floors and ceilings using rays to prevent the player from clipping through walls

(most of this is explained in the first two videos)

The plan was to modify the wall jump script he used in video # 6, but there was a problem when sliding down walls that did not have a ledge below it. The player would continue a slow decent despite there not being a wall anymore.

The first thing I change was to make the physics function make horizontal detections even if there is no horizontal inputs; but that lead to another issue that I can't pin down.

  • the player will now only wall slide if you hold the directional key toward the wall!!!!!!

I have checked the x transform to ensure that the player isn't moving unintentionally, but for some reason the wall detection just stops unless you hold the directional key toward the wall. The absolute worse part is that it only stops some of the time.

Is this a limitation of using Rays? Or is something else going on? Any help would be greatly appreciated

Comment
Add comment · Show 4
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 SunnyChow · Jan 13, 2014 at 04:52 AM 0
Share

I think it will be easier to help you if you show your script

avatar image SunnyChow · Jan 13, 2014 at 04:55 AM 0
Share

i think it's just a logic problem more than the limitation of Ray

avatar image Nony42 · Jan 13, 2014 at 05:08 AM 0
Share

The code to move horizontally:

 private float IncrementTowards(float n, float target, float a) 
     {
         if (n == target) 
         {
             return n;    
         }
         else 
         {
             float dir = $$anonymous$$athf.Sign(target - n); // must n be increased or decreased to get closer to target
             n += a * Time.deltaTime * dir;
             return (dir == $$anonymous$$athf.Sign(target-n))? n: target; // if n has now passed target then return target, otherwise return n
         }
     }

 targetSpeed = Input.GetAxisRaw("Horizontal") * speed;
 currentSpeed = IncrementTowards(currentSpeed, targetSpeed,acceleration);


avatar image Nony42 · Jan 13, 2014 at 05:12 AM 0
Share

currentSpeed is then put into a Vector 2 and then passed into this function:

 move(Vector 2 amountTo$$anonymous$$ove)
         {
             amountTo$$anonymous$$ove.x = deltaX;
 
             for (int i = 0; i<3; i ++) 
             {
                             // creates the rays
                 float dir = $$anonymous$$athf.Sign(deltaX);
                 float x = p.x + c.x + s.x/2 * dir;
                 float y = p.y + c.y - s.y/2 + s.y/2 * i;
                 
                 ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
                 Debug.DrawRay(ray.origin,ray.direction);
                 
                 if (Physics.Raycast(ray, out hit,$$anonymous$$athf.Abs(deltaX) + skin, collision$$anonymous$$ask)) 
                 {
                     // Get Distance between player and ground
                     float dst = Vector3.Distance (ray.origin, hit.point);
                     
                     // Stop player's horizontal movement after co$$anonymous$$g within skin width of a collider
                     if (dst > skin) 
                     {
                         deltaX = dst * dir - skin * dir;
                     }
                     else 
                     {
                         deltaX = 0;
                     }
 
                     movementStopped = true;
                     break;    
                 }
             }
         }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nony42 · Jan 15, 2014 at 04:50 AM

I have found a fix! First I made horizontal detection possible even if there is not input. Then in the PlayerPhysics script where it checks for wall collisions, I created another if() statement.

 if(Physics.Raycast(ray, out hit, 0.006f, collisionMask))
 {
         if(hit.collider.tag == "Wall Jump" && !grounded)
         {
                 canWallHold = true;
         }
 }

This was necessary because if there was no horizontal input, the original ray distance check would not reach far enough to detect the wall which is why the distance I checked was just a little bit longer than the skin's distance.

Then in the PlayerController script I also changed all the checks for wall holding to one if() statement

 if(playerPhysics.canWallHold == true)
 {
         wallHolding  = true;
 }
 else
 {
         wallHolding = false;
 }
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

19 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

Related Questions

Raycast error 1 Answer

Make an Enemy Shoot through Raycast 1 Answer

change object material on mouse click. 2 Answers

Negative Positions Breaks Raycasts 1 Answer

How to touch select 3D objects 2 Answers


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