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

Rotation

Discussion in 'Scripting' started by klassicsamm, May 8, 2020.

  1. klassicsamm

    klassicsamm

    Joined:
    Apr 22, 2020
    Posts:
    5
    Hey guys, new here, hope everyone is enjoying their Unitication!!

    I am trying to rotate a turret around another object that resembles its real world pivot and it just doesnt work. Here is a screen shot of my cannon (Parent, with a few child objects like turret, base, turret holders, pivot)

    View attachment 616546

    Here is the code:

    Transform turretPivot = _turretPivot.transform;
    Vector3 centerPoint = new Vector3(turretPivot.position.x, turretPivot.position.y, turretPivot.position.z);
    _turret.transform.RotateAround(centerPoint, -direction * Vector3.right, 10f * Time.deltaTime);

    I hope this situation hasn't been posted a million times, although I did a lot of research and didn't find the exact situation.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    You need to nest the objects inside each other - base - pivot - barrel and only rotate one axis of each (base and pivot). And yes, it's been done and answered a million times over. So perhaps improve your Google skills before upping Unity's :)
     
  3. klassicsamm

    klassicsamm

    Joined:
    Apr 22, 2020
    Posts:
    5
    You didn't get my question. All those objects are nested. When rotating horizontally, all is fne, when rotating the turret vertically it rotates based on the parent object which is the base of the cannon. I want it to be rotated based on its own pivot.

    If you didn't get my question, please do not reply. Maybe improve your comprehension skills? Thanks for nothing.
     
  4. klassicsamm

    klassicsamm

    Joined:
    Apr 22, 2020
    Posts:
    5
    Aaaaand I figured it out myself! The easiest way to achieve this is to add an empty object and set its local coordinates to where you want the turret to pivot, then set the turret as its child.

    _turretPivot = GameObject.Find("TurretPivot");
    _turretPivot.transform.Rotate(0f, 0f, 1f, Space.Self);
     
  5. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Why didn't I get your question? Because you posed your quesion in a way that was not easy to understand and lacked vital information. You will note that your OP neither posed a question, nor stated what the problem was, merely that it "just wasn't working". It would have helped if your stated "I want it to be rotated based on it's own pivot". And you can achieve that by nesting (as I wrote and you subsequently discorered on your own), or moving the origin of the barrel to the pivot point, as would be clear if you studied the demo project hat I linked to (which is pretty much the goto script for starting any turret script today -- meaning it's one of the top google links, hence my "Google Skill" snark).

    It helps if you regard any qustion you write as YOUR responsibility to make sure it's unambiguous, complete, and on point. And in conversations, let me give you a tip that has served me well for the past 20 years on my job: never say "you don't understand me" or "you misunderstand me". Bear the burden of responsibility and say 'perhaps I was unclear - let me rephrase the question". And do try to refrain from unneccessary malice on anyone who takes the time to respond to your question, especially if they go to the trouble and look up, and link to, working examples. I may be a smug idiot, but your response reflects even worse on you.

    Just to show you that I do not bear grudges, a tip to further improve your code:
    Do not use GameObject.Find in any code that is executed regularly, use it once in Start(). Better yet, if possible define _turretPivot as [SerializeField] or public, and assign it in Editor.
     
    Last edited: May 9, 2020
    Yoreki likes this.
  6. klassicsamm

    klassicsamm

    Joined:
    Apr 22, 2020
    Posts:
    5
    Sorry didn't read. Bye.
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    That's just rude.
    You came here asking for help, with a badly worded, or outright non-existant question. You still got a correct answer (admittedly the comment about googling was as unnecessary as it was true), but accused the person giving it of misunderstanding you. You then claimed to have figured it out yourself and proceeded to ignore further general advice with that comment?
    I can already tell that you wont have a great time asking for or getting answers in a public forum like this.