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 Garsum · Sep 27, 2016 at 07:55 AM · collidersraycastinglookatrayhit

Find a collision point from inside the object.

I'm working on a game for vive room scale. I am also new to unity. In other words, have a great time.

My player is in the room surrounded by a round (really a flattened sphere) force field. When he points a controller at the force field I would like to have a game object from the the outside of the force field to slam onto the the force field. And then, as the player moves the controller around the object will slither around the outside of the field to wherever the players pointing relative to the players head.

Set up: create a sphere gameobject with a rigid body and mesh collider that surrounds the room scale playspace.

User action: user pulls trigger and the object sticks to the sphere on the line created by lining up the players eyes and the controller. The user can do this from anywhere within the playspace. The controller does not have to be close to the force field.

Issue: rays do not seem to impact upon a object if created inside the object.

The following is my solution. I placed the script on the object this is moving on the sphere. One possible problem with my solution is if an foreign object gets in between the force field and my arbitrary Outerpoint the object will go and stick to the foreign object instead of force field.

I feel like there should be a much better and more efficient way of accomplishing this.

I would appreciate any advice,pointers, reference material to help me improve. ing UnityEngine; using System.Collections;

 public class Aiming : MonoBehaviour  // Attached to Game Object to move around the Sphere
 {
     public GameObject Player;  // Camera (eye)
     public GameObject UseControler; //Controller (right)
   
     // Use this for initialization
     void Start() 
     {
     }
 
     // Update is called once per frame
     void Update()
     {
         // Create Vector between Player and Controller
         var AimingVector = UseControler.transform.position - Player.transform.position;
         // Create outbound Ray that passes thru Sphere
         Ray AimingRay = new Ray(Player.transform.position, AimingVector);
         // Find a point outside the Sphere that in on the ray
         var OuterPoint = AimingRay.GetPoint(7);
         // Create a vector between the outer point and the Player
         var PositioningVector =  Player.transform.position - OuterPoint; 
         // Create a ray inbound toward the sphere to the Player
         var PositioningRay = new Ray(OuterPoint, PositioningVector);
         RaycastHit hit; 
         if (Physics.Raycast(PositioningRay, out hit)) // Do the ray cast
             // did we hit anything... (In this setup we always will, but check anyway)
             if (hit.rigidbody != null)
             {
                 // find the point on the Sphere where the player to controller to the Sphere intercepts
                 transform.position = hit.point;  
                 // rotate gameobject to face player
                 transform.LookAt(Player.transform.position, Vector3.up);
             }
     }
  
 }
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

73 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

Related Questions

Problem with raycasting from center of the collider 1 Answer

ARCore cannot detect a button hit inside the canvas 0 Answers

Terrain Collider Hit 0 Answers

The raycast is allways looking to the camera. 0 Answers

Raycasting problem, colliders not being recognized? 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