- Home /
How to mark collision point of plane and object?
Hi,
I am trying to create a Virtual Reality game to measure how people intercept objects by tracking their eye movements with the FOVE VR headset. I am also using a Phantom Haptics device which has allowed me to restrict participants field of movement to a certain area, constrained to the X and Y axes. I have been trying to write a script that displays the point where the ball passes through the plane that participants are restricted to so that participants can see how close they were to catching the object but have had no success as of yet. I read somewhere that using custom physics in Unity can cause issues with registration in situations like this. Below is the code used to calculate the ball trajectories. I did not write this code as I am carrying on from someone else's work and am fairly new to programming. So, basically what I would like to know is: is there any code or procedure that could help me display the interception point of the plane and ball and then disappear a second later, each time the ball passes through a plane? Or is the custom physics perhaps breaking the object detection? I have been using OnTriggerEnter but have not been able to get anything to happen at all. Sorry for the long post and thank you for any help.
public void launchBall(Vector3 targetPoint, Vector3 startPoint, float initialVelocity)
{
float g = -Physics.gravity.y;
if (useTrajectory)
{
ball.transform.position = startPoint;
ball.useGravity = true;
ball.velocity = calculateTrajectory(targetPoint, startPoint, initialVelocity);
}
else
{
gameObject.transform.position = new Vector3(0, 0, 5.0f);
ball.velocity = new Vector3(0, 0, 0);
}
}
Vector3 calculateTrajectory(Vector3 target, Vector3 startPoint, float v0)
{
float t_x = target.x - startPoint.x;
float t_y = target.y - startPoint.y;
float t_z = target.z - startPoint.z;
float g = -Physics.gravity.y;
float theta = Mathf.Atan(
(Mathf.Pow(v0, 2) -
Mathf.Sqrt(Mathf.Pow(v0, 4) -
g * (g * Mathf.Pow(t_z, 2) + 2 * t_y * Mathf.Pow(v0, 2))))
/ (g * t_z)
);
float v_z = -v0 * Mathf.Cos(theta);
float v_y = -v0 * Mathf.Sin(theta);
float vst = v0 * Mathf.Sin(theta);
float tInf = (v_y + Mathf.Sqrt(Mathf.Pow(v_y, 2) + 2 * g * (-t_y)))
/ g;
float v_x = t_x / tInf;
return new Vector3(v_x, v_y, v_z);
}
}
Does your moving object has a rigidbody attached? Otherwise triggers or collisions won't work
Answer by ChrisUOB · May 14, 2019 at 10:03 PM
Thanks for your reply, @Pangamini. I do, I also have sphere collider on the moving object and a mesh collider on the plane. I have also created a practice project to practice working with triggers, in which I have moved a ball through a plane using a simple controller, and displayed a message in the log OnTriggerEnter which works just fine for my practice project. However, when I try and apply the same idea to my main project, nothing happens when the ball passes through the plane.