Search Unity

Rotate Object around itself (tunnel)

Discussion in 'Getting Started' started by unity_DwpE30i390jLrQ, Jul 30, 2021.

  1. unity_DwpE30i390jLrQ

    unity_DwpE30i390jLrQ

    Joined:
    Jan 21, 2019
    Posts:
    9
    Hello Guys.

    I'm trying to make a tunnel (where a spaceship will travel into).

    The controls will rotate THE TUNNEL, for the player to avoid obstacles.

    Thing is, the tunnel is rotating around a wrong axis and not itself.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Controller : MonoBehaviour
    6. {
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         if (Input.GetKey(KeyCode.A)) {
    18.             transform.Rotate (0, 50 * Time.deltaTime, 0, Space.Self);
    19.         }
    20.  
    21.         if (Input.GetKey(KeyCode.D)) {
    22.         transform.Rotate(0, -50 * Time.deltaTime, 0, Space.Self);
    23.         }
    24.     }
    25. }
    26.  
    This code is within the tunnel (which is the parent of the cylinder model and an obstacle.

    Here's the behaviour

     
  2. unity_DwpE30i390jLrQ

    unity_DwpE30i390jLrQ

    Joined:
    Jan 21, 2019
    Posts:
    9
    Wrong pivot point, I mean
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    See at the top there, just to the right of your translate/rotate/scale buttons, there's a button that says "Center"? Click that so it says "Pivot" and select your tunnel object.

    The point where the three axes meet is the pivot point setup for that object. If you made this model, you didn't set its origin point to be centered around its long axis. If you got this model from somewhere else, you'll have to either find a different one or open it in a modeling tool to edit the origin yourself.

    You could just make the tunnel a child of an empty gameobject and try to position it so that the empty object's origin is aligned with where the tunnel's origin should be, but that sounds like too much of a PITA to me.
     
    unity_DwpE30i390jLrQ likes this.
  4. unity_DwpE30i390jLrQ

    unity_DwpE30i390jLrQ

    Joined:
    Jan 21, 2019
    Posts:
    9
    OMG THAT WAS IT!
    I've altered my Blender moldel. Thank you SO MUCH <3
     
    Schneider21 likes this.