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);
}
}
}