Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Letail · Oct 08, 2017 at 11:49 AM · vector3raycastingcubeanglesquads

Problem raycasting to quads at certain angles

Hello,

I'm trying to teleport my player to a face of a cube made out of quads. And I want to align the player's transform.up with the face of the quad. The thing is, this works well for some faces and doesn't for others. My script works like this:

  1. I set the player's position to be the same as the quad.

  2. I ray cast in all 6 directions (vector3.up, vector3.down, ect...)

  3. If the ray cast hits, that means the quad is in that direction and I change the transform.up of the player so that he is standing/aligned on/with the quad.

This works for some faces but not for others. Some quads are not hit by my raycasting. However, if I put one of the quads that is not being hit, at a slight angle (say, Rotation(0,183,0) instead of (0,180,0)) then the raycast hits that quad and all works perfectly. And I don't know why.

I've tried everything, even offsetting the raycast angle, but it still doesn't work.

Here's the code:

 Vector3[] vectorOrientations = new Vector3[6];
 
  void Awake ()
     {
         //Populating the array with all the Vector3.orientations.
         vectorOrientations[0] = Vector3.up;
         vectorOrientations[1] = Vector3.down;
         vectorOrientations[2] = Vector3.right;
         vectorOrientations[3] = Vector3.left;
         vectorOrientations[4] = Vector3.forward;
         vectorOrientations[5] = Vector3.back;
 
         playerHeight = transform.localScale.y;
     }
 public void MoveDestination(Transform destinationFace)
     {
         //send the player to another face
         transform.position = destinationFace.transform.position;
 
         RaycastHit hit;
 
         //Cycle through every orientation..
         for (int i = 0; i < vectorOrientations.Length; i++)
         {
             //..and do a raycast to see if it hits anything..
             if (Physics.Raycast(transform.position, vectorOrientations[i], out hit, 3.0f, 1, QueryTriggerInteraction.Ignore))
             {
                 //..other than the player.
                 if (hit.transform.tag != "Player")
                 {
                     //align the vector up of the player with the quad
                     transform.up = hit.transform.forward * -1;
 
                     //raise the player from the floor by adding their height to themselves
                     transform.Translate(Vector3.up * playerHeight);
                     
                 }
             }
         }
     }

(The small cubes are paired triggers that send the player back and forth to the other faces of the cube)

alt text

question.png (144.6 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

0 Replies

· Add your reply
  • Sort: 

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

89 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 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 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 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

Why is my y-axis bugging? 1 Answer

Obtain angle from given rotation 1 Answer

How to detect if player is facing towards enemy's back 2 Answers

Flipping a cube while including the environment 0 Answers

Need help setting a vector behind a character 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