- Home /
Rotate object
Hey there everyone,
I've ran in to a problem: I have a Prefab that follows my mouse and I am trying to rotate it (around the Y-axis). I've looked everywhere and tried everything but my prefab keeps spinning around in big circles (not around the Y-axis). This is my current rotation code: if(Input.GetKey(KeyCode.R)){ Instance.transform.Rotate(0,10,0); }
Is anyone able to help me out? Note: the script is attached to my camera, I think it might have something to do with this (?)
Edit: I fixed the script that causes my object to follow the cursor, it's working properly now. Thanks all!
Answer by robertbu · Jan 06, 2014 at 07:02 PM
Script needs to be attached to your object, not the camera. Plus you should use Time.deltaTime:
function Update() {
if(Input.GetKey(KeyCode.R)){
transform.Rotate(0,60.0 * Time.deltaTime,0);
}
}
So it wouldn't be possible to have it on a different object?
You can, but you have to reference the game object you are spinning. Lets say the object you want to spin has the name 'Player', you could do:
var player : Transform;
function Start() {
player = GameObject.Find("Player").transform;
}
function Update() {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.R)){
player.Rotate(0,60.0 * Time.deltaTime,0);
}
}
I am using the following code to reference the GameObject.
public GameObject Instance;
and
Instance = Instantiate(InstancePrefab, camera.ScreenToWorldPoint(Input.mousePosition), Quaternion.identity) as GameObject;
Then the rotation code would be:
function Update() {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.R)){
Instance.transform.Rotate(0,60.0 * Time.deltaTime,0);
}
}
It does not seem to work. I get the same result as always: http://i.imgur.com/U1hHxvU.gif
It just starts making circles.
Your answer
![](https://koobas.hobune.stream/wayback/20220613125713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotating Cuboid Around Pivot 2 Answers
A node in a childnode? 1 Answer
rotate object around another object 1:1 1 Answer
how to reorient the axes ? 1 Answer
Rotate One Object Around Another 1 Answer