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 Fr0stbite · Mar 09, 2014 at 01:04 PM · raycastrotateturnsample

Raycast Rotate Help

i try to rewrite this code from sample assets because i want to raycast from the right and left side but the raycast doesn't rotate with the player

 // Ground Check:
 
         // Create a ray that points down from the centre of the character.
         Ray ray = new Ray(transform.position, -transform.up);
         Ray right_ray = new Ray(new Vector3(transform.localPosition.x+0.5f,transform.localPosition.y,transform.localPosition.z), -transform.up);
         Ray left_ray = new Ray(new Vector3(transform.localPosition.x-0.5f,transform.localPosition.y,transform.localPosition.z), -transform.up);
         
         // Raycast slightly further than the capsule (as determined by jumpRayLength)
         RaycastHit[] hits = Physics.RaycastAll(ray, capsule.height * jumpRayLength );
         RaycastHit[] right_hits = Physics.RaycastAll(right_ray, capsule.height * jumpRayLength );
         RaycastHit[] left_hits = Physics.RaycastAll(left_ray, capsule.height * jumpRayLength );
 
            
         float nearest = Mathf.Infinity;
     
         if (grounded || rigidbody.velocity.y < 0.1f)
         {
             // Default value if nothing is detected:
             grounded = false;
             
             // Check every collider hit by the ray
             for (int i = 0; i < hits.Length; i++)
             {
                 // Check it's not a trigger
                 if (!hits[i].collider.isTrigger && hits[i].distance < nearest)
                 {
                     // The character is grounded, and we store the ground angle (calculated from the normal)
                     grounded = true;
                     nearest = hits[i].distance;
                     //Debug.DrawRay(transform.position, groundAngle * transform.forward, Color.green);
                 }
             }
             //Check every collider hit by the right
             for (int i = 0; i < right_hits.Length; i++)
             {
                 // Check it's not a trigger
                 if (!right_hits[i].collider.isTrigger && right_hits[i].distance < nearest)
                 {
                     grounded = true;
                     nearest = right_hits[i].distance;
                 }
             }
             //Check every collider hit by the left
             for (int i = 0; i < left_hits.Length; i++)
             {
                 // Check it's not a trigger
                 if (!left_hits[i].collider.isTrigger && left_hits[i].distance < nearest)
                 {
                     grounded = true;
                     nearest = left_hits[i].distance;
                 }
             }
         }

i've wrote x axis but it doesn't turnalt text

asd.jpg (56.1 kB)
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

1 Reply

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

Answer by robertbu · Mar 09, 2014 at 03:07 PM

I don't believe you want to use transform.localPosition as part of this calculation. Raycasting is a world positing thing, so I suggest you convert your point from local to world space using Transform.TransformPoint(). Something like:

 Ray right_ray = new Ray(transform.TransformPoint(new Vector3(0.5f,0,0), -transform.up);
 Ray left_ray  = new Ray(transform.TransformPoint(new Vector3(-0.5f,0,0), -transform.up);

Also note that your use of '-transform.up' here will cast the ray out the bottom of the cube no matter what the rotation. If you are using this for ground sensing, consider changing that to Vector3.down.

Comment
Add comment · Show 3 · 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 Fr0stbite · Mar 09, 2014 at 03:16 PM 0
Share

error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

avatar image robertbu · Mar 09, 2014 at 03:27 PM 0
Share

Edited the answer to fix the problems. As I move back and forth between C# and Javascript, I sometimes forget the 'new' and the 'f' in C#.

avatar image Fr0stbite · Mar 09, 2014 at 03:50 PM 0
Share

thanks it works :)

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

20 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

Related Questions

Third Person Cross Hair Question 1 Answer

Weird rotation shapes 1 Answer

Activating a single Prefab with the same tag using Raycast. 0 Answers

Raycast not rotating with object / always have raycast ray a certain way no matter what? 2 Answers

Rotate model, turn around only, no bend? 0 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