Search Unity

How to rotate object around one axis ?

Discussion in 'Scripting' started by artiny, Dec 16, 2016.

  1. artiny

    artiny

    Joined:
    Oct 24, 2016
    Posts:
    7
    hy
    i want to rotate my drone propeller when i hit the keyboard.
    Now look like this,when i run this script.
    When i run the game the propeller when is alone ,then rotate good. BUt when i attech to the body of drone, when i fly around, the propeller is going from the horizontal aligment of the drone.Can you pls help in script ...to stay propeller inside the holes when is rotate. {freeze the axis}.


    http://i.imgur.com/StLCSUV.png

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class rotateRotor : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.  
    9.  
    10.     // Update is called once per frame
    11.     void Update () {
    12.         if (Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W) || Input.GetKey (KeyCode.S))
    13.             //transform.Rotate (Vector3.up * 30 * Time.deltaTime);
    14.             //transform.Rotate(0, 90, 0, Space.Self);
    15.             transform.RotateAround(Vector3.down,180);
    16.     }
    17. }
     
    Last edited: Dec 17, 2016
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    1) use code tags
    https://forum.unity3d.com/threads/using-code-tags-properly.143875/

    2) I seldom use the transform rotate functions since really they're just wrappers around quaternion math.

    Instead I'd just use the rotation. quat.

    Since we're nested, lets use the 'localRotation' so that we're rotating local.

    Code (csharp):
    1.  
    2. public float speed = 30f; //degrees per second
    3.  
    4. void Update()
    5. {
    6.     transform.localRotation *= Quaternion.AngleAxis(speed * Time.deltaTime, Vector3.up);
    7. }
    8.  
    That should probably do it...

    note I "*=" the quat, this is because multiplying quaternions is like appending one on the other.

    If you had a quat that was 10 degrees around up, and another 20 around up, then multiplied you'd get 30 around up.

    So here I'm just saying "append a small change in degrees around up", but to the localRotation, so that up means up as the propeller sees it, not as the world sees it.
     
  3. artiny

    artiny

    Joined:
    Oct 24, 2016
    Posts:
    7
    Thank you you answer,...I tried your code, but the roation is very similar - is look like this:

    http://i.imgur.com/F52aXkd.jpg



    I tried 3 different things:
    1. - added a simple cube (green) created in unity - and attached to the drone body inside the holes. Your script is worked good, like i want to make roation.

    2. - added propeller what i found on the "turbosquid.com" not worked

    3. added a 3ds max propeller what was I created, but is same not working, is going out from the horizontal aligment. - I also set the pivot to the center in the 3ds max of the propeller.

    SO what is the problem?
     
    Last edited: Dec 17, 2016
  4. A_World_Maker

    A_World_Maker

    Joined:
    Apr 21, 2018
    Posts:
    214
    My guess, and I am no scripter, the propellers are all spinning around a single pivot point. Your base model only has a single pivot point. In Unreal I would set each model independently (not attached with the base model, once they are all spinning correctly, I would group all the props and the base model so they all move at the same time. I don't know how to do that in Unity.