- Home /
How to use raycast here?
I am using a picture here to explain problem.
I want my raycast to look like this when I move around the sphere. Should I use rotation or something else.
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);
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().
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