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

transform.Rotate doesn't react on my objects.

Discussion in 'Scripting' started by Anon1234, Apr 29, 2015.

  1. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class JustRandomMovementOfNPCs : MonoBehaviour {
    5.     public int theRandomizerX;
    6.     public int theRandomizerY;
    7.    
    8.     public Vector2 startMarker;
    9.     public Vector2 endMarker;
    10.     public float time = 0.0f;
    11.  
    12.     void Awake() {
    13.         theRandomizerX = Random.Range (1, 33);
    14.         theRandomizerY = Random.Range (-1, -20);
    15.     }
    16.  
    17.     void Update() {
    18.         transform.Rotate(Vector3.right * Time.deltaTime);
    19.         Vector2 selfCurrentVector = new Vector2(this.transform.position.x, this.transform.position.y);
    20.         Vector2 arriveAt = new Vector2 (theRandomizerX, theRandomizerY);
    21.         transform.position = Vector3.Lerp (selfCurrentVector, arriveAt, time);
    22.         time += Time.deltaTime / 500;
    23. //        this.transform.Rotate = new Quaternion (0, 0, 3, 0);
    24.  
    25.         if (Vector2.Distance (selfCurrentVector, arriveAt) < 1.5f)
    26.             ChangeVars ();
    27.     }
    28.  
    29.     void ChangeVars() {
    30.         theRandomizerX = Random.Range (1, 33);
    31.         theRandomizerY = Random.Range (-1, -20);
    32.         time = 0;
    33.     }
    34. }
    First line after Update(); doesn't work. I have script which puts about 20 prefabs on the map (prefab has this script attached), they're squares and circles and everything. But none of them rotate (I would notice square rotating, trust me ;) )
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    try

    Code (csharp):
    1.  
    2. gameObject.transform.Rotate(Vector3.right);
    3.  
    just guessing, might need gameObject (self) or Time.deltaTime is making it too slow to notice.
     
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    I think your transform.Rotate value would be way too small to notice:
    you are telling it to rotate Vector3.right * Time.deltaTime degrees which is like 0.017 degrees. It would take 21,000 update calls to just rotate it once.
    Try multiplying it by some constant like
    Vector3.right * Time.deltaTime * 20.0f;
     
    Anon1234 likes this.
  4. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Yea, I just did it, and I finally can notice it :p

    Uhm, could you help though? The game is actually 2D, and whenever I try to change Vector3.up and everything else, it keeps it twisting in 3rd dimension, if I change it to Vector2.right etc., it keeps just twisting it in another directions, what I actually need it that it will smoothly rotate (counter)clockwise. I need to rotate it on Z-axis.
     
  5. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    ive never done 2D games in Unity. Is the camera looking down at the world x-z plane?

    most likely I think you need to rotate it around it's y-axis then:

    transform.Rotate(new Vector3(0, 10.0f, 0), Space.Self);

    This should rotate it around it's up axis (y axis) 10 degrees with respect to itself.
    if the camera is looking down at it, it will appear to rotate clockwise.
    try that out.
     
    dunity1 and Anon1234 like this.
  6. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    transform.Rotate(new Vector3(0, 0, 10f), Space.Self);

    Made it work, thanks :).
     
  7. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    ok, just make sure that the object's y-axis is always the same direction as the world's y-axis, otherwise the object will start rotating strangely. I think in your case you should only rotate things around their y-axis for now. if you rotate something around the x or z axis, suddenly the y-axis will no longer be pointing up and you will have issues.
     
  8. dunity1

    dunity1

    Joined:
    Jul 21, 2018
    Posts:
    1
    I am generating objects at random location , I want them to spin continuously after getting generated for nice visual effect. I tried newApple.transform.Rotate(new Vector3(0,0, 10.0f), Space.Self); but it does not work.
    Please help. Thank you .
    here is my code
    while (applesRemaining < appleTarget ){
    do{
    spawnLocation = new Vector3 (Random.Range(-10f,10f), Random.Range(-4f,2f), 0f);

    }while(Physics.CheckSphere (spawnLocation, 2f) == true);

    spawnRotation = new Vector3(0f, 0f, Random.Range(0f,360f));
    GameObject newApple = Instantiate (apple, spawnLocation,
    Quaternion.Euler(spawnRotation)) as GameObject;

    newApple.transform.Rotate(new Vector3(0,0, 10.0f), Space.Self);

    applesRemaining++ ;

    }