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 Fox-Handler · Jun 04, 2014 at 07:07 PM · distancephysics.raycast

The Distance in Raycast?

Hi guys, I was following a tutorial online and I'm getting a little confused with the Distance in the Raycast. So pretty much what is going on is that DeltaY which is also being used as the gravity (-20) is being used as the distance for the raycast Mathf.Abs (deltaY) So if, we end up actually Checking.. the distance, then it seems like the distance 20 is omitted or something. So can someone kind of give an explanation on that. Is the number 20 just being ignored?

 [RequireComponent(typeof(BoxCollider))]
 
 public class PlayerPhysics : MonoBehaviour 
 {
     public LayerMask collisionMask; 
 
     private BoxCollider collider;   
     private Vector3 size;           
     private Vector3 center;        
 
     private float boarder = .005f;
 
     [HideInInspector]
     public bool grounded;
 
 
     Ray ray;
     RaycastHit hit;
 
     void Start ()
     {
         collider = GetComponent<BoxCollider>();  
         size = collider.size;                     
         center = collider.center;                  
     }
 
 
     public void Move(Vector2 moveAmount)  
     {
         float deltaY = moveAmount.y;                   
         float deltaX = moveAmount.x;
         Vector2 position = transform.position;        
          
         grounded = true;
 
         for (int i = 0; i<3; i++) 
         {
             float dir = Mathf.Sign (deltaY); 
             float x = (position.x + center.x - size.x/2) + size.x/2 * i; 
             float y = position.y + center.y + size.y/2 * dir;
 
             ray = new Ray(new Vector2(x,y), new Vector2(0,dir)); 
             Debug.DrawRay(ray.origin,ray.direction);
             if (Physics.Raycast(ray, out hit, Mathf.Abs (deltaY), collisionMask)) 
             {
             //Get Distance between player and ground
             float Dist = Vector3.Distance (ray.origin, hit.point); 
 
             
                 if(Dist > boarder)                                
                 {
                     deltaY = Dist * dir + boarder;  /
                     
                 }
                 else
                 {
                     deltaY = 0;                 
                 } 
 
                 grounded = false;
 
                 break;                         
             }
         }
 
         Vector2 finalTransform = new Vector2(deltaX,deltaY);  
         transform.Translate (finalTransform);
     }                                      
 }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by rutter · Jun 04, 2014 at 07:27 PM

The distance argument sets the maximum distance your ray will travel during the cast.

For example, if you set a distance of 20, your ray specifies an origin and direction, and the cast will continue up to 20 units (or until it hits something).

One possibility: it looks like your ray is coming from the center of the player (or whatever object you're moving). You might instead want it to come from the outermost edge of that object.

Suppose for a moment that your player has a radius of 5 units, and moves down 1 unit. You cast a ray from the center of the player, down 1 unit... surprise, surprise, it doesn't hit anything, because that's still inside the player.

There are many ways you could adjust for height, but the simplest is probably something like this:

 float playerRadius = 5f; //wild guess, use something smart
 y += playerRadius * dir;

You could also check the player's collider bounds. Some colliders, such as Box2D or capsule colliders, offer specific height information.

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 Fox-Handler · Jun 04, 2014 at 07:40 PM 0
Share

Oh so its the max distance of the Raycast. This was actually a box, and it was set up so that a for loop, would set the x position on the left middle and center of the box, and then actually be brought to the bottom with the y position so that when the ray was actually cast, a ray would be co$$anonymous$$g from 3 different points from the bottom of the box.

avatar image
0

Answer by robertbu · Jun 04, 2014 at 07:28 PM

I don't see where anything you have here directly relates to gravity. I'm guessing that moveAmount is related to gravity, but it likely is also multiplied by Time.deltaTime. But none of that matters. This is the line to take a look at:

 float dir = Mathf.Sign (deltaY); 

Mathf.Sign() returns either -1 or 1. So your Raycast() will be in the direction you are moving and have a distance of 1.0.

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 Fox-Handler · Jun 04, 2014 at 07:51 PM 0
Share

yea threw me off for a while.

 if(Dist > boarder)                                
                 {
                     deltaY = Dist * dir + boarder;  /
                     
                 }

this line right here is about the only thing I am kind of struggling with to understand. But I do get the gist of it.

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

22 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

Related Questions

Problems with Physics.Boxcast ( reward for fixing 10€ via paypal ) 1 Answer

iam using raycast.hit function.am trying to move vehicle over the terrain ups and down?? 0 Answers

Raycast distance affected by momentum of character 0 Answers

Tracking distance after Instantiate 2 Answers

Making a 2d "radar" for space game 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