- Home /
raycast object instance offset?
Lads,
I'm using raycast and mousedown to swap one gameobject for another. All is working, but the instantiated object comes in offset (I'd like it to come in the exact position of the gameobject it's replacing). I think the problem has to do with the hit.point of the ray or the destruction of the gameObject... not sure. Anyway, here's the code:
function Update () {
var hit : RaycastHit;
if(Physics.Raycast (transform.position,
transform.forward, hit, 500)) {
if (hit.collider.gameObject.tag=="Block"
&& Input.GetMouseButtonDown(0) == true) {
var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 500;
Debug.DrawRay (transform.position, forward, Color.green);
//var instance2:GameObject = Instantiate (Resources.Load
//("Detonator_explosion1"),transform.position, transform.rotation);
var instance:GameObject = Instantiate (Resources.Load
("building_stage_2_test"),hit.point, Quaternion.identity);
Destroy (hit.collider.gameObject);
}
}
}
Any thoughts?...
ps - the pivot locations of the object being instanced and the object being destroyed match.
Thx,
Stef
Firstly, you should get rid of the "== true" since "Input.Get$$anonymous$$ouseButtonDown(0)" returns true or false anyway (so the check is unnecessary).
$$anonymous$$ore importantly, put the Input.Get$$anonymous$$ouseButtonDown(0) test as the outermost "if", otherwise you're wasting ressources every frame casting rays, ins$$anonymous$$d of just the single(!) frame where the mouse button is actually pressed.
@Wolfram... what do you mean by the 'outermost' if... thanks.
Just move the Input.Get$$anonymous$$ouseButtonDown(0) test out of your block, and make it the very first thing that's tested in Update(). This way, if the button is not pushed (which is probably 99% of your game time), this is the only test that will be performed, and everything within that "if" is never executed. Your current code does a Physics.Raycast() every frame, and then, several lines below, tests the mouse button, and says, "oh well, the button wasn't pressed anyway, so that's that".
@Wolfram... like this?:
function Update () {
if Input.Get$$anonymous$$ouseButtonDown(0)
var hit : RaycastHit;
if(Physics.Raycast (transform.position,
transform.forward, hit, 500)) {
if (hit.collider.gameObject.tag=="Block"
{
var forward : Vector3 = transform.TransformDirection(Vector3.forward) * 500;
Debug.DrawRay (transform.position, forward, Color.green);
//var instance2:GameObject = Instantiate (Resources.Load
//("Detonator_explosion1"),transform.position, transform.rotation);
var instance:GameObject = Instantiate (Resources.Load
("building_stage_2_test"),hit.transform.position, Quaternion.identity);
Destroy (hit.collider.gameObject);
Answer by Wolfram · Jun 18, 2012 at 09:11 PM
Well, you're instancing it at the exact hit.point
position. Use hit.transform.position
instead, which is the pivot of the object that was hit.
I changed the hit.point to:
var instance:GameObject = Instantiate (Resources.Load ("building_stage_2_test"),hit.transform.position, hit.transform.rotation);
but am still getting the same result?...
Thx
Hm, the only explanation I can think of is that the objects really have a different pivot. If you drag both the original object and your "building_stage_2_test" into the Hierarchy view (so that they have identical values in their transform), are the really placed on top of each other, the way you want them to?
Or you actually hit a different object, interfering with it. Is the original object composed of multiple GameObjects/colliders/sub-objects?
Your answer
![](https://koobas.hobune.stream/wayback/20220613072946im_/https://answers.unity.com/themes/thub/images/avi.jpg)