transform.Rotate doesn't react on my objects.

using UnityEngine;
using System.Collections;

public class JustRandomMovementOfNPCs : MonoBehaviour {
    public int theRandomizerX;
    public int theRandomizerY;
   
    public Vector2 startMarker;
    public Vector2 endMarker;
    public float time = 0.0f;

    void Awake() {
        theRandomizerX = Random.Range (1, 33);
        theRandomizerY = Random.Range (-1, -20);
    }

    void Update() {
        transform.Rotate(Vector3.right * Time.deltaTime);
        Vector2 selfCurrentVector = new Vector2(this.transform.position.x, this.transform.position.y);
        Vector2 arriveAt = new Vector2 (theRandomizerX, theRandomizerY);
        transform.position = Vector3.Lerp (selfCurrentVector, arriveAt, time);
        time += Time.deltaTime / 500;
//        this.transform.Rotate = new Quaternion (0, 0, 3, 0);

        if (Vector2.Distance (selfCurrentVector, arriveAt) < 1.5f)
            ChangeVars ();
    }

    void ChangeVars() {
        theRandomizerX = Random.Range (1, 33);
        theRandomizerY = Random.Range (-1, -20);
        time = 0;
    }
}

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 :wink: )

try

gameObject.transform.Rotate(Vector3.right);

just guessing, might need gameObject (self) or Time.deltaTime is making it too slow to notice.

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;

1 Like

Yea, I just did it, and I finally can notice it :stuck_out_tongue:

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.

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.

2 Likes

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

Made it work, thanks :).

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.

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++ ;

}