Fix the center of the object

Hi,

I have a problem. In my app, I instantiate an object and put it as a child of an empty object, when I try to move it with my finger the instantiated object goes to the top or other position around my finger but not where I put this.

So what could be the problem? I already set the handle position to the center and tried to see some change settings “Global” or “Local”, but nothing changed.

Thank you in advance for the help!

Here’s how you can find out:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

You must find a way to get the information you need in order to reason about what the problem is.

There aren’t errors. And I’m using the hit position to place the object so the object should be placed where I hit, but this doesn’t happen with ALL the objects. Only a few of them.

Should I do a script to center the handle position?

Then figure that out. How are those objects different? Pick one that works, pick one that doesn’t, put each one at (0,0,0)… do they both look right??

Yeah, I noticed that! It’s the object’s position that causes the problem. What I can do it’s just fixing it at the runtime for the object that I instance from the web.

The problem is that I don’t know what to do because If I move the object part it doesn’t look the same. I attached some screens of the structure in my hierarchy.

P.S. What I move with the finger is the first parent object named “The Object”





If you have an object mis-placed, your choices are largely:

  • edit that object in a 3D editor and set its pivot correctly (best)

  • make a prefab out of it and parent it to another object, adding the offset yourself (easiest)

  • install ProBuilder in Unity, import and “probuilderize” the object so you can edit the geometry in Unity

1 Like

That could be great, but I don’t have control over this because my app should allow the user to upload his own object/character.

Practically, I need to do the second option that you told me about, but as we know we can’t create a prefab at runtime.

At this point, I don’t think there is a solution for this. Am I wrong?

As much as ProB drives me nutty with its issues, this right here is one reason i keep it around at all times. Even the greats like Synty (used as an eg because, well everyone knows them) have issues with models not always being configured correctly for use. ‘Points at synty vehicles steering wheels / axis. In what world do steering wheels turn like that?’ .

ProB makes some stuff easy fast without having to fire up additional heavy software just to get the job done.

1 Like

The information has to come from somewhere to correct the error.

If your user is giving you bad data, that’s between you and the user.

Tell them the pivot has to be correct.

I suppose you could make your own editor window to let the user manually correct the defective pivot, then you can use that pivot to generate your own offset parent at runtime.