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 mrhussain0334 · Oct 30, 2013 at 01:51 PM · raycastmove an objectsphere raycast

How to use raycast here?

I am using a picture here to explain problem. alt text

I want my raycast to look like this when I move around the sphere. Should I use rotation or something else.

l.png (895 B)
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
0

Answer by Tomer-Barkan · Oct 30, 2013 at 01:56 PM

Yes, you can use rotation of 90 degrees on the vector that connects your position with the center of the circle. Or you can just use the direction of your velocity if you use physics (in circular motion, the velocity is always 90 degrees to the radius).

This code will work if the circle is on the X/Z plane, and is attached to the object that moves around the circle, where "circleCenter " is a vector representing the position of the center of the circle.

 Vector3 toCenter = circleCenter - transform.position;
 Quaternion rotate90degrees = Quaternion.AngleAxis(90, Vector3.up);
 Vector3 raycastDirection = rotate90degrees * toCenter;
 Physics.Raycast(transform.position, raycastDirection);
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
avatar image
0

Answer by robertbu · Oct 30, 2013 at 03:08 PM

If the small sphere is going to be arbitrarily walking around the big sphere, then you either need to use local coordinates to the small sphere, or you have to have a point of reference. The general solution for sphere walking is to align the small sphere with a vector between center of the large sphere and the center of the small sphere, so that the small sphere is standing 'up' at its position on the sphere. Then you can use the transform.forward of the small sphere for your raycast.

 var q = Quaternion.FromToRotation(smallSphere.up, smallSphere.position-bigSphere.position)
 smallSphere.rotation = q * smallSphere.rotation;
 if (Physics.Raycast(smallSphere.position, smallSphere.forward)) {

If you have a point of reference, then you can use Vector3.Cross() to find the vector. The point of reference is the axis the small sphere is using when it rotates around the big sphere. The point of reference might be a world vector like Vector3.forward, or it could be related to the position of the camera with respect to the sphere. To calculate the raycast:

 var rayDir = Vector3.Cross(axis, smallSphere.position - bigSphere.position);
 if (Physics.Raycast(smallSphere.position, rayDir)) {

Note to get the raycast in the direction you expect, you might have to either negate the axis or swap the two parameters in the Vector3.Cross().

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 sdgd · Nov 01, 2013 at 06:41 PM 0
Share

BTW @robertbu doesn't that give south pole madness? Quaternion.FTR, ...

and this is kinda duplicate Question

well I've made it this way:

             Ray ray = new Ray(transform.position, $$anonymous$$F.V3DirectionF(transform.position, Planet.transform.position));
 //            Debug.DrawRay(transform.position, $$anonymous$$F.Direction(transform.position, Planet.transform.position)*500, Color.red);
             RaycastHit hit;
             if (Planet.collider.Raycast(ray, out hit,
                                         $$anonymous$$F.V3DistanceF(transform.position, Planet.position) )  ){
                 transform.rotation = Quaternion.LookRotation( Vector3.Cross(transform.right, hit.normal) , hit.normal);
             }

it's without south pole madness

and if he could read my code there he'd know that by now but apparently C# is giving him so much headache than JS to me

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

17 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

Related Questions

Disabling lights collision 2 Answers

Creating Objects Through Raycast 1 Answer

Help: 3d, (Set, tilted, perspective camera) move object to mouse relative to the ground/objects. 1 Answer

Move object with mouse without passing through colliders 0 Answers

Raycast to determine certain game objects? 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