- Home /
Instantiated an Prefab on a Spherical Object at mouse cursor position
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.
Answer by zmar0519 · Mar 11, 2011 at 08:49 PM
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!
Answer by DaveA · Mar 11, 2011 at 08:43 PM
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
Answer by Caiuse · Mar 11, 2011 at 08:55 PM
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!