Instantiated an Prefab on a Spherical Object at mouse cursor position

alt text

The example image demonstrates what I'm trying to achieve.

I tried using Recasting from the mouse position but I was getting strange results such as the object instance being far from the surface of the sphere. What would be the best way to go about Instantiating an object at the cursors position on a spherecial object.

Thanks - C

Another strange result is the object instantiating slightly offset form where the mouse clicks.

Sounds like your prefab isn't centered around 0, 0, 0 then.

3 Answers

3

Try this. I am doing a similar thing:

var building : GameObject;

function Update () 
{
    if(Input.GetMouseButtonDown(0))
    {
        Build();
    }
}
function Build()
{
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit, 100)) 
        {
            Debug.DrawLine (ray.origin, hit.point);
            Instantiate(building,hit.point,Quaternion.identity);
        }
}

The Debug.DrawLine is something i have been ;looking to do for ages but not known how. thanks so much!

Sounds like the collider is not spherical, is it? If a mesh, same mesh for render as collider? Also are you using Physics.Raycast or Collider.Raycast?

It was me being stupid the Prefab models pivot point was complete off. Thanks for you answer though, it'll do! :D

Check all the prefabs pivot points making sure there not out of line of the mesh. In other words check if the object is centered at 0,0,0 otherwise you'll get my problem!

Right, this answer is more than confusing. Static classes are never serialized by almost any serialization system. This includes Unity's serialization system. The question was specifically about singletons and editor scripts but the example is neither a singleton nor an editor script.