• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Raycasthit is sometimes returning gameobject name when I use hit.gameobject.comparetag. 0 Answers

The raycast is allways looking to the camera. 0 Answers

How to Access the Size component of a Collider from a Ray 1 Answer

Raycast from Camera passes BoxColliders while Mouse is dragging with high volocity above. 0 Answers

Raycasting problem, colliders not being recognized? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges