Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate GameObject by pivot

Discussion in 'Scripting' started by Burton-kun, Jul 20, 2019.

  1. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    Hello everyone.
    I'm trying to rotate a certain GameObject that has it's center in the lower left corner. I need to rotate it by a center calculated on runtime. The rotation is made by using direction vectors but I can't find any method in the Unity API that seems to rotate a GameObject by a certain pivot using vectors as source of direction change.
    Does anybody knows how to help me?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    One approach:

    - Create a new GameObject at the desired pivot point
    - Parent the object you want rotated to this GameObject
    - Rotate the root GameObject however much you want
    - Unparent the object in question
    - Destroy the original root GameObject.

    Alternately if you want to do it pure-math, the steps are as follows:

    - subtract the pivot position from your object
    - rotate your object
    - add the pivot position back into your object
     
    DonLoquacious likes this.
  3. Burton-kun

    Burton-kun

    Joined:
    Aug 6, 2015
    Posts:
    50
    I solved by implementing your first suggestion, thank you very much ;)
     
    Kurt-Dekker likes this.