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 /
  • Help Room /
avatar image
0
Question by alagrad94 · Jun 14, 2019 at 01:27 PM · rotationraycastraycasting

Raycast issues while rotating a GameObject

My game has a stack of tiles. They are all child objects of the board. Tiles are "Selectable" based on whether they have neighbors to the left/right/up. I am using a raycast to determine if there is another tile in those directions.

I added a simple script on the board so that it can be rotated around the X-axis.

 public float rotateSpeed = 20;
 public void OnMouseDrag() {
      float rotateX = Input.GetAxis("Mouse X") * rotateSpeed * Mathf.Deg2Rad;
      transform.Rotate(Vector3.up, -rotateX);
 }

My raycast script looks like this and is attached to the tile prefab (these become the child objects of the object being rotated).

 IsBlockedLeft = Physics.Raycast(transform.position, Vector3.right, .9f);
 IsBlockedRight = Physics.Raycast(transform.position, Vector3.left, .9f);
 IsBlockedUp = Physics.Raycast(transform.position, Vector3.up, .5f);

Before I added the rotation, my raycasts were slightly different, but in experimenting this morning I have found that there is no functional difference in the way they are now and the way they were. I had them like this

 IsBlockedLeft = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), .9f);
 IsBlockedRight = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.left), .9f);
 IsBlockedUp = Physics.Raycast(transform.position, transform.TransformDirection(Vector3.up), .5f);  

The raycast vector values are based upon the size of the tiles. They are cubes with a size of 0.75, 0.3, 1, so the vector is just supposed to go to the center of the next cube. I'm not sure why the left/right reversal works, neither the parent nor the tiles have any rotation on them. But, it is detecting neighboring tiles correctly this way.

The raycast is working perfectly until I start rotating the board and then they all go haywire and either start hitting things when there is not another tile there, or stop hitting the ones that are there. I know this because the "IsBlocked" values are public bools that I can see in the inspector and the tiles also change color as those values change. So as I start to rotate the board, I can see them changing. I suspect that it has something to do with needing to adjust the raycast vector based on the rotation, but I haven't been able to find a way to do this correctly.

It's probably also important to note that when I'm rotating the board (the parent object) if you watch the tiles (child objects) in the inspector the position, rotation, and scale all remain unchanged, only the parent's transform values change.

To try and confirm my suspicions, I had the debugger draw all 3 rays. You can see in this image how the angle they are going changes with the rotation of the board. Before the rotation, the left and right rays were going straight out from the center, as they are supposed to do. alt text

Any insights/suggestions etc... would be greatly appreciated.

screen-shot-2019-06-14-at-90623-am.png (87.7 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
0

Answer by alagrad94 · Jun 14, 2019 at 09:09 PM

So, I finally found a solution to this issue. However, if anyone else has any ideas, I would still like to hear them. I'm not very experienced with Unity, so I would like to see if I solved it the way more experienced Unity developers would have...

I figured out that the problem wasn't that the rays were rotating with the board, it's that they WEREN'T rotating with the board. So, as the board and all the child objects (tiles) rotated, the rays were still being cast in their original direction. Here was my solution, there were 3 parts to it:

First, I changed the direction of my rays to be based on the board's transform instead of the tile's transform and adjusted their length slightly. The Code now looks like this:

 Ray leftRay = new Ray(transform.position, transform.parent.TransformVector(Vector3.left));
 Ray rightRay = new Ray(transform.position, transform.parent.TransformVector(Vector3.right));
 Ray upRay = new Ray(transform.position, transform.parent.TransformVector(Vector3.up));
 
 IsBlockedLeft = Physics.Raycast(leftRay, .6f);
 IsBlockedRight = Physics.Raycast(rightRay, .6f);
 IsBlockedUp = Physics.Raycast(upRay, .4f);

Second, I added an "IsRotating" bool to the board. And control it in OnMouseDrag() and OnMouseUp().

 public void OnMouseDrag() {
    IsRotating = true;
    float rotateX = Input.GetAxis("Mouse X") * rotateSpeed * Mathf.Deg2Rad;
    transform.Rotate(Vector3.up, -rotateX);
 }
 public void OnMouseUp() {
      IsRotating = false;
 }

And finally, I increased the size of the box collider on the tiles to 1.25/1.25/1.25. I had to do this because on the ends of some of the rows of tiles, there are ones that are offset and cover the ends of two rows. The tiles behind these were still experiencing a little bit of a problem of missing the tile that was only covering half of their left or right side. But, increasing the size of the collider solved this and testing hasn't shown any other issues arising from that so far.

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

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

Raycast BulletDrop 0 Answers

Need help ironing out this weird error in Raycast2D and Vector2.Reflect, for a billiard game test 1 Answer

RayCast2D and RayDraw errors 0 Answers

Converting a rotation(degree) to a vector3(usable in a RAY) 1 Answer

Select from multiple character to move 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