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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by joe_carrot · Feb 03, 2013 at 10:24 PM · physicsraycastground

Using raycasting to make a character NOT move off a platform

I've got this. The Debug.DrawLine() correctly draws the line, but the actual raycast appears to be behaving differently than the Line.

 // This shoots a ray forward and down to make sure that the character is not going to fall down a level.
 // Returns true if there is ground below in fron of the character
 function testGround()
 {
     var hit : RaycastHit;
     Debug.DrawRay(transform.position + Vector3.up, transform.forward - (Vector3.up * 1.5), Color.yellow);
     if(Physics.Raycast(transform.position + Vector3.up, transform.forward - (Vector3.up), 1.5))
     {
         grounded = true;
     }
 
 }

Basically, my characters using this script are always setting themselves as grounded or not grounded, regardless of what terrain they are approaching.

I'm pretty sure it has something to do with my Physics.Raycast line, but hat has been one of the biggest stumbling blocks for me in learning.

Any help is appreciated!

edit used Vector3.up for both casts. Got rid of unsused hitInfo.

Comment
Add comment · Show 1
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 joe_carrot · Feb 03, 2013 at 10:08 PM 0
Share

Here's an image of the Line in action: http://cl.ly/image/343G0g2m0D1l

1 Reply

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

Answer by Bunny83 · Feb 03, 2013 at 10:32 PM

You draw a totally different ray than what you use for the actual raycasting,,,

First you use Vector3.up when drawing the ray and transform.up when you do the raycast. In most cases they should be the same (unless you tilt your character), however it makes no sense to use two different vectors.

Next thing is the drawray call draws the ray at an angle of around 56° while your raycast casts at 45° downwards.

Ok, just simplify the calculations by assuming our character isn't rotated. So it's forward vector points towards the world's z-axis (0,0,1)

when you calculate this:

     transform.forward - (Vector3.up)  // or this transform.forward - (transform.up)

You end up with a vector like this:

     (0,0,1) - (0,1,0) = (0,-1,1)

since it's a direction vector it would get normalized internally to a length of 1. So internally Unity will use:

     (0,-0.707,0.707)

this vector points at 45° downwards since the amount on the forward axis is equal to the amount on the y axis.
However when you calculate:

     transform.forward - (Vector3.up * 1.5)

You actually calculate this:

     (0,0,1) - ((0,1,0) * 1.5) == (0,0,1) - (0,1.5,0) == (0,-1.5,1)

This vector points more downwards than forward so the angle is greater. If you normalize the vector (length is ~1.8027) you get (0,0.8320,0.5547). The ASin of the y value will give you the angle:

     ASin(0.707) == 45°
     ASin(0.8320) == 56.309°

If you want the exact same vector for both, the drawing and the raycast, you should normalize the vector manually and then scale it with your desired length (in your case 1.5):

     var dir = transform.forward - Vector3.up;
     dir = dir.normalized;
     Debug.DrawRay(transform.position + Vector3.up, dir * 1.5, Color.yellow);
     if(Physics.Raycast(transform.position + Vector3.up, dir, 1.5))
Comment
Add comment · Show 2 · 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 joe_carrot · Feb 03, 2013 at 10:39 PM 0
Share

That's true, I forgot I was using transform.up ins$$anonymous$$d of Vector3.up on the second one (I was testing something). Edited to have them both be the same up vector.

What I don't understand is the 56° vs 45° degree issue.

As I said, I am new and dealing with local to world vectors and the like is still somewhat difficult to wrap my head around.

avatar image joe_carrot · Feb 04, 2013 at 03:17 AM 0
Share

Thanks so much. This is a very clearly written response, and though I don't get it fully yet, I believe I will be able to get it completely soon.

Also it works so theres that!

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

10 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

Related Questions

Unable to set collision layer from scrip 0 Answers

Is Physics.Raycast deterministic? 1 Answer

Make a rolling ball always on ground without falling when reaching the edges of the map 1 Answer

Make Ray hit own collider 1 Answer

changing guiTexture color with raycast 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