How To Change The Pivot Point Of An Object In Unity

18,575

There are two commonly used approaches: define your own geometry and anchors.

1. Defining Your Own Geometry

If you are savvy with model building software (e.g., Maya or Blender), they often support tools allowing you to specify the center-point of the geometry. Importing these models into Unity, I believe, preserves those center-points.

2. Anchors

Perhaps the simplest way is to use an anchor-object. Simply create an empty object to use as the transform-parent of the model you want to rotate at an offset, translate the child object appropriately from the anchor, and apply whatever transforms you were originally doing to the anchor object (not the child).

Share:
18,575
BeastCoder
Author by

BeastCoder

I am a hobbyist who enjoys all things coding. Recommended Books: Automate the Boring Stuff With Python

Updated on June 04, 2022

Comments

  • BeastCoder
    BeastCoder almost 2 years

    I have a question. I would like to make the pivot point of an object be at the end of an object rather than the center. It is for an AI that I am creating and I need an invisible rectangle to look at the player, I'm doing this so that I can detect if there is an obstacle - I'm already using raycasting but sometimes at certain angles the line doesn't sense the obstacle, therefore I need the invisible rectangle too. Although, the problem is that sometimes the rectangle is picking up an obstacle from behind it since it is rotating around the center of itself rather than the end. It looks like this:

    enter image description here

    I am using C# by the way.

    And the code I am using to make the rectangle look at the player is:

    transform.LookAt(player.transform.position);
    

    So how, using the code I have can I make the pivot point of the rectangle be at the end of it so that the rectangle wont pick up collisions behind it.

    print("All Help is Appreciated!");

    :D

    EDIT:

    I did what Zackary said and it works fine except that it is backwards - how can I fix that?

    enter image description here

  • Zackary
    Zackary over 5 years
    Try negating the relative position of the rectangle. At a glance, it looks like you put the rectangle on the point of the enemy that looks away from the player (making it so the enemy appears to be facing backwards). Edit: got my rectangle and enemy backwards.
  • Shaun Dreclin
    Shaun Dreclin about 4 years
    Seems kind of inappropriate to self-promote something you're selling on here.
  • Michael Sander
    Michael Sander over 2 years
    I agree with @ShaunDreclin. Please note that there are multiple other assets in the store that do exactly this for free.