- Home /
Can I get the Tangent(as Vector3) of a sphere at the hit point with raycast?
Hello everyone! I have to plot models on a sphere by simply draging prefabs and plotting them on a sphere. The drag is working fine, I need to raycast to know the point where the object will be plotted. But I need the tangent at the hit point so that the plotted object sit well on the sphere(the y of the object will be the same direction as the tangent and not up in world space) Is there method that gets that tangent or I have to calculate it? Any possible solution will help:)
Can you give an example or a screenshot? A tangent could still be rotated around the hit point. Also how exactly should your object be rotates? Relatively to your hit point, in which direction do you want the y-axis and the z-axis?
The y-axis is usually the objects up vector, when you aligh it to a tangent the object will lay flat. Are you sure you want a tangent and not the normal?
An image that shows what you need would save 100s of words.
Answer by Clunk · May 29, 2012 at 11:49 PM
Here is an example, only the raycast part you need, so implement this into your script. You can test this by creating a large sphere, then creating a cube a few meters above the sphere, and attaching this script to the cube. The cube or rectangle, whatever object you have the script attached to, will attach to whatever its down facing ray hits. You can of course modify this how you wish. Hope this helps resolve your issue.
function Update()
{
var hit : RaycastHit;
var down = transform.TransformDirection(Vector3.down);
if(Physics.Raycast(transform.position, down, hit))
{
transform.position = hit.point + hit.normal;
transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
}
}
I am having problems with rotation, the newly instantiated prefab keeps rotating! even its far from the sphere! thanks btw for the hint, it must help after i figure out where's my error
if object is continuously rotating, you must have a transform.rotation+ or transform.Rotate in Update()... Post up the code that contains the continuous rotation.
Dude everything working properly, thanks for the hints, just the positioning worked only with hit.point and not hit.point + hit.normal