Hi I have character performs an action where an object appear in the scene. I want this object to be rotating in the y direction none stop. I am not sure how to make it rotate continuously your advice will be very helpful.
The following is the code I use to instantiate the object:
var obj1 = Instantiate (theObjects[Random.Range(0,14)], holefront, Quaternion.FromToRotation(Vector3.up, transform.forward));
Thank you.
Hi,
For rotating an object continuously try this.
transform.Rotate(Vector3.up, Time.deltaTime);
Hope this will work.
Hi,
Thanks for your respond.
I tried with the above code I added to the script, but there is no changes at all.
I want the obj1 to be rotating. I am still stuck figuring this out…
Hi there ,
I used this code in my trial project and its working properly. There might be some other problem .
In that project i need to rotate the object around X-axis so i did this .
using UnityEngine;
using System.Collections;
public class rotate : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
transform.Rotate(Time.deltaTime,20,20);
}
}
When i used transform.Rotate(Time.deltaTime,20,20); my scene rotate to a different direction.
How do i get my obj1 only to rotate continuously.
I have also tried obj1.Rotate(Time.deltaTime,20,20);, I get this error on the console MissingMethodException: Method not found: ‘UnityEngine.GameObject.Rotate’.
Im not sure what I doing wrong, I hope I have made myself clear what I am trying to achieve.
Thank you
I have a quick question which may sound weird, do i have to have colliders or rigidbody for my objects i am trying to rotate.
You can use one of format of the rotation function: Rotate (axis : Vector3, angle : float, relativeTo : Space = Space.Self)
For example, transform.Rotate(0, 45 * Time.deltaTime, 0); //rotate 45 degrees
Make sure that you attach the script to your prefab.
I think that this format may work as well: obj1.transform.Rotate(0, 45 * Time.deltaTime, 0);
Thanks.
But my script is not attached with my prefab. I have about 20 items that appears randomly and i have this script attached to my player.
So when player dig the object appears randomly, currently the object rotate to position but it is not rotating continuously. What variable makes the object to rotate?
Is there any reason not to use a prefab? It is the most efficient technique to instantiate a gameObject, especially multiple gameObjects.
Basically, everyone has suggested almost the same thing and it should work as expect. In your case, you can attach the following script to the gameObject theObject.
// Rotates the object to which it is attached.
function Update ()
{
transform.Rotate (0, 45 * Time.deltaTime, 0);
}
Create a new JavaScript and insert this code:
public var Speed : Vector3;
function Update()
{
var t = Time.deltaTime;
transform.Rotate( Speed.x * t, Speed.y * t, Speed.z * t );
}
Then add the script to your GameObject. Setup the rotation through the Speed variable.
Thank you so much Yhoko. Once I added the script to each of the GameObject then it worked.
You really saved my day… 